我正在尝试使用beautifulsoup从Craigslist获取数据PID和价格。我写了一个单独的代码,它给了我CLallsites.txt文件。在这个代码中,我试图从txt文件中获取每个站点,并获得前10页中所有条目的PID。我的代码是:
from bs4 import BeautifulSoup
from urllib2 import urlopen
readfile = open("CLallsites.txt")
product = "mcy"
while 1:
u = ""
count = 0
line = readfile.readline()
commaposition = line.find(',')
site = line[0:commaposition]
location = line[commaposition+1:]
site_filename = location + '.txt'
f = open(site_filename, "a")
while (count < 10):
sitenow = site + "\\" + product + "\\" + str(u)
html = urlopen(str(sitenow))
soup = BeautifulSoup(html)
postings = soup('p',{"class":"row"})
for post in postings:
y = post['data-pid']
print y
count = count +1
index = count*100
u = "index" + str(index) + ".html"
if not line:
break
pass
我的CLallsites.txt如下所示:
craiglist网站,位置(Stackoverflow不允许使用cragslist链接发布,所以我无法显示文本,如果有帮助,我可以尝试附加文本文件。)
当我运行代码时,我会得到以下错误:
追踪(最近一次通话):
文件“reading.py”,第16行,in
html=urlopen(str(sitenow))
文件“/usr/lib/python2.7/urllib2.py”,第126行,在urlopen中
return _opener.open(url、数据、超时)
文件“/usr/lib/python2.7/urllib2.py”,第400行,打开
回应=自我_打开(请求,数据)
文件“/usr/lib/python2.7/urllib2.py”,第418行,在_open中
'打开',请求)
文件“/usr/lib/python2.7/urllib2.py”,第378行,在_call_chain中
结果=函数(*args)
http_open中的文件“/usr/lib/python2.7/urllib2.py”,第1207行
return self.do_open(httplib.HTTPConnection,req)
文件“/usr/lib/python2.7/urllib2.py”,第1177行,在do_open中
引发URLError(错误)
urlib2.URLError(URL错误):
你知道我做错了什么吗?