1

私は 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')   
4

2 に答える 2

0

何らかの理由で<a>タグ内のテキストを読みたい場合は、次のようにすることができます。

postings = [x.text for x in soup.find("div", {"class":"content"}).findAll("a", {"class":"hdrlnk"})]
print(str(postings).encode('utf-8'))

これにより、長さが 100 のリストが返されます。

于 2015-11-04T21:03:48.630 に答える