我的代码不想输出提取的数据

问题描述 投票:0回答:2

我想从网站(德语黄页)中提取多个链接,但是当我点击运行按钮时,我的代码什么都不做。我的刮刀没有反应,也没有给出错误警告。我该如何解决?问题出在哪儿?

我尝试了reddit首页上的代码,工作正常,我得到了一个数据输出。但它在感兴趣的网页https://www.gelbeseiten.de/arzt/heilbronn-neckar上没有成功。

在这个screenshot,你可以看到我想要提取的内容。

在id =“gs_treffer”的div-tag中,我想从article-tag中提取data-href链接。

import urllib.request
from bs4 import BeautifulSoup

url = "https://www.gelbeseiten.de/arzt/heilbronn-neckar/"

#download the URL and extract the content to the variable html 
request = urllib.request.Request(url)
html = urllib.request.urlopen(request).read()

#pass the HTML to Beautifulsoup.
soup = BeautifulSoup(html,'html.parser')

#get the HTML of the table called site Table where all the links are                 displayed
main_table = soup.find("div",attrs={'id':'gs_treffer'})

#Now we go into main_table and get every a element in it which has a     class "title" 
links = main_table.find_all("article", class_="data-href")

#from each link extract the link 
#List to store a dict of the links we extracted

extracted_records = []
for link in links:
url = link['data-href']
record = {
    'url':url
    }
extracted_records.append(record)
print(extracted_records)
python web-scraping
2个回答
1
投票

你想摆脱find_all方法中的类_ =“data-href”参数,因为“data-href”不是一个类。

links = main_table.find_all("article")

我现在得到一个带网址的dicts列表:

[{'url': 'https://www.gelbeseiten.de/gsbiz/b1f40122-810e-4e51-9915-0e5ac98e32a5'}, {'url': 'https://www.gelbeseiten.de/gsbiz/44beddcf-a428-452c-ade1-a2e4e7807b23'}, {'url': 'https://www.gelbeseiten.de/gsbiz/d3268940-07f3-41c4-bcbd-e33d341ba379'}, {'url': 'https://www.gelbeseiten.de/gsbiz/3fe695df-8695-4940-81f5-bee17fbdf168'}, {'url': 'https://www.gelbeseiten.de/gsbiz/f8a8f769-6806-4742-b62b-b46753bcebe0'}, {'url': 'https://www.gelbeseiten.de/gsbiz/aa19c150-da60-4ef6-ba00-ef672fbf34da'}, {'url': 'https://www.gelbeseiten.de/gsbiz/3e7b5aa8-7ae0-4779-a4ad-e2a51b4d7315'}, {'url': 'https://www.gelbeseiten.de/gsbiz/5d9e76b0-85ea-4316-88b2-b25f417b6d58'}, {'url': 'https://www.gelbeseiten.de/gsbiz/ca1d47eb-22e3-44bf-95de-0cf93f39761a'}, {'url': 'https://www.gelbeseiten.de/gsbiz/caf662da-d8ad-43b0-83c5-8b6c962195ba'}, {'url': 'https://www.gelbeseiten.de/gsbiz/346bf41b-e415-47cc-9609-788311322ab6'}, {'url': 'https://www.gelbeseiten.de/gsbiz/9f73cee9-a1dc-47b8-ab9e-e1855512cdc6'}, {'url': 'https://www.gelbeseiten.de/gsbiz/057ba124-aa45-40b9-a033-bf83ecc7c3ef'}, {'url': 'https://www.gelbeseiten.de/gsbiz/69b0e77e-9ae4-4f8f-82f7-9aa7cbab1a75'}, {'url': 'https://www.gelbeseiten.de/gsbiz/7a3de200-08c3-48ee-ac0c-fcfc183d35c3'}]

0
投票

你的线links = main_table.find_all("article", class_="data-href")不起作用。我认为因为你不是在寻找基于它的类的元素。

如果用links = main_table.find_all("article")替换该行,则脚本将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.