我在使用BeautifulSoup时发现,即使代码或连接没有任何变化,解析一个页面有时也要花费相当长的时间。有什么想法吗?
from bs4 import BeautifulSoup
from urllib2 import urlopen
#The particular state website:
site = "http://sfbay.craigslist.org/rea/"
html = urlopen(site)
print "Done"
soup = BeautifulSoup(html)
print "Done"
#Get first 100 list of postings:
postings = soup('p')
发布于 2015-11-05 05:03:49
如果出于某种原因,您想要阅读<a>
标记中的文本,可以这样做。
postings = [x.text for x in soup.find("div", {"class":"content"}).findAll("a", {"class":"hdrlnk"})]
print(str(postings).encode('utf-8'))
这将返回一个长度为100的列表。
发布于 2017-02-08 16:39:52
postings = soup('p')
这段代码不太好。计算机必须检查每一行以确保p标签在其中。一个接一个。
aTag = soup.findAll('a',class_='result_title hdrlnk')
for link in aTag:
print(link.text)
https://stackoverflow.com/questions/17756582
复制相似问题