【发布时间】:2017-09-04 10:32:14
【问题描述】:
我的目标是编写一个可以从网站检索特定数据的 Python 脚本。
具体来说,我必须提取这些数据:
<span class="street-address" itemprop="streetAddress">191, Corso Peschiera</span>
和
<div itemprop="telephone" class="tel elementPhone">0184 662271</div>
当然只有号码和地址!
虽然我尝试提取普通的“div”或“a”或“href”,但我没有任何问题,但我无法改进我的研究。
这是我的代码...我无法写入文件,除非我只将soup.find_all('a') 之类的参数传递给bs4:
from bs4 import BeautifulSoup
import requests
r = requests.get('https://www.paginegialle.it/ricerca/lidi%20balneari/Torino?')
data = r.text
soup = BeautifulSoup(data,"html.parser")
dia = soup.find_all('<div itemprop="telephone" class="tel elementPhone"></div>')
for link in soup.find_all('<div itemprop="telephone" class="tel elementPhone"></div>'):
print (dia)
documento=open("mbsprovalive.csv","w")
documento.write(dia)
documento.close()
我该如何解决这个问题?
【问题讨论】:
-
您在哪里读到
soup.find_all('<div itemprop="telephone" class="tel elementPhone"></div>')会起作用?你看过这个调用的返回值吗?你看过文档吗? -
首先阅读文档:crummy.com/software/BeautifulSoup/bs4/doc/#find-all。
find_all()的第一个位置参数是标签名称,而不是一些 html sn-p。要根据您传入合适的关键字参数或作为attrsarg 的字典的属性进行过滤。 -
@Tomalak 通过文档,我能够构建一个脚本,该脚本可以找到所有“div”或所有“a”,但不是我在答案中所说的更复杂的表达式。
-
那你还没有阅读文档。
标签: python beautifulsoup python-requests