我想从使用美丽的Craigslist获取数据PID和价格。我写了一个单独的代码给我的文件CLallsites.txt。在此代码中,我试图从txt文件中获取每个网站,并获得前10页中所有条目的PID。我的代码是:美丽的汤URL错误
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上的网站,位置(#2不允许与克雷格列表的联系张贴所以我不能显示的文字,我可以尝试的附加文本文件是否有帮助)
当我运行代码,我得到以下错误:
回溯(最近通话最后一个):
文件“ reading.py”,第16行,在 HTML =的urlopen(STR(sitenow))
文件 “/usr/lib/python2.7/urllib2.py”,线路126,在的urlopen 返回_opener.open(的URL,数据,超时)
文件 “/usr/lib/python2.7/urllib2.py”,线路400,在开放 响应= self._open(REQ,数据)
文件“/ USR /lib/python2.7/urllib2.py”,线418,在_open '_open',REQ)
文件 “/usr/lib/python2.7/urllib2.py”,线路378,在_call_chain 结果= FUNC(*参数)
文件 “/usr/lib/python2.7/urllib2.py”,线1207,在http_open 返回self.do_open(httplib.HTTPConnection,REQ)
文件“/usr/lib/python2.7/urllib2.py”,线1177,在do_open 提高URLError(ERR)
urllib2.URLError:
什么我做错了任何想法?
您可以在urlopen之前放置“打印sitenow”并查看打印出来的内容吗? – Bemmu 2013-03-26 23:58:19