写的一个爬新浪微博的爬虫,出了点小疑点
写的一个爬新浪微博的爬虫,出了点小问题
登录模块是从网上找的,登录之后想广度优先爬取关注关系:从用户A开始,爬A关注的用户B,C,D...爬完A后,同样的步骤爬B,有点像二叉树的层序遍历。现在就是循环递归这块不会写了,请大家看看
报错是IndexError: list index out of range 位置就是在14行findall(html)[0], 抓取网页用户名称的位置
------解决思路----------------------
那你就模糊一点,不要用单引号,用.*?来模糊的匹配一点,能匹配到后再一点点优化正则表达式。
登录模块是从网上找的,登录之后想广度优先爬取关注关系:从用户A开始,爬A关注的用户B,C,D...爬完A后,同样的步骤爬B,有点像二叉树的层序遍历。现在就是循环递归这块不会写了,请大家看看
def start(url):
is_read = 0
total = 0
urls = []
def main(url, is_read):
url_tmp = url
for i in range(1,2):
pageNum = i
page = '?page=' + str(pageNum)
url = url_tmp + page
req = urllib2.Request(url)
html = urllib2.urlopen(req).read().replace('\\', '')
result = re.compile('<a class.*?"S_txt1" target=.*?href="(.*?)" >(.*?)<', re.S).findall(html)
print re.compile('<title>(.*?)的', re.S).findall(html)[0]
#owner_name = re.compile('<meta content="(.*?),', re.S).findall(html)[0]
with open('check.txt', 'w') as g:
g.write(html)
with open('result.txt', 'a') as f:
for item in result:
# item[0]--site item[1]--name
site = ' http://weibo.com' + item[0]
url_next = 'http://weibo.com' + item[0] + '/follow'
content = item[1] + site + '\n'
if is_read == 0:
urls.append(url_next)
f.write(content)
print u'微博用户序号:0'
main(url, 0)
for m in range(0, len(urls)):
print u'微博用户序号:' + str(m + 1)
main(urls[m], 1)
start('http://weibo.com/.../follow')
报错是IndexError: list index out of range 位置就是在14行findall(html)[0], 抓取网页用户名称的位置
------解决思路----------------------
那你就模糊一点,不要用单引号,用.*?来模糊的匹配一点,能匹配到后再一点点优化正则表达式。