【发布时间】:2015-11-02 19:10:34
【问题描述】:
我正在尝试从网页/网站中提取数据。这是我的代码:
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re
webpage=urlopen('http://www.xxxxxxxxx.com').read()
patFinderTitle=re.compile('<title>(.*)</title>')
patFinderLink=re.compile('<link rel.*href="(.*)"/>')
findPatTitle=re.findall(patFinderTitle,webpage)
findPatLink=re.findall(patFinderLink,webpage)
listIterator=[]
listIterator[:]=range(2,16)
for i in listIterator:
print findPatTitle[i]
print findPatLink[i]
print "\n"
articlepage=urlopen(findPatLink[i]).read()
divbegin=articlepage.find('<div class="">')
article=articlepage[divbegin:(divbegin+1000)]
soup=BeautifulSoup(article)
paralist=soup.findAll('<p>')
for i in paralist:
print i
我想列出网页中的标题和所有链接。当我运行脚本时,它会引发错误:
Traceback (most recent call last):
File "justdialcrawl.py", line 21, in <module>
print findPatTitle[i]
IndexError: list index out of range
我尝试在 Google 上搜索,但找不到答案。
【问题讨论】:
-
您为什么使用非常旧的
BeautifulSoup版本?运行pip install beautifulsoup4,然后在你的程序中使用from bs4 import BeautifulSoup。
标签: beautifulsoup web-crawler python-requests urllib python-2.5