[英]url errors in beautiful soup
我正在尝试使用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行,位于html = urlopen(str(sitenow))
urlopen中的文件“ /usr/lib/python2.7/urllib2.py”,行126返回_opener.open(URL,数据,超时)
文件“ /usr/lib/python2.7/urllib2.py”,第400行,打开响应= self._open(req,data)
_open'_open'中的文件“ /usr/lib/python2.7/urllib2.py”,第418行,req)
_call_chain中的文件“ /usr/lib/python2.7/urllib2.py”,行378 = func(* args)
http_open返回self.do_open(httplib.HTTPConnection,req)中的文件“ /usr/lib/python2.7/urllib2.py”,行1207
do_open中的文件“ /usr/lib/python2.7/urllib2.py”,行1177提高URLError(err)
urllib2.URLError:
关于我在做什么错的任何想法吗?
我不知道sitenow
的内容是sitenow
,但看起来它是无效的URL。 请注意,URL使用斜杠而不是反斜杠(因此,该语句类似于sitenow = site + "/" + product + "/" + str(u)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.