爬取自己微博的内容
采取爬取移动端微博的办法,页面看上去就是老旧手机的微博网
import time
import requests
from bs4 import BeautifulSoup as bs
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
cookie = '自己的 cookie'
headers = {
'User-Agent': user_agent,
'Cookie': cookie
}
f = open('xxx.txt', 'ab')
page = 1
num = 8
while page < num:
url = 'https://weibo.cn/u/' + '微博id' + ?page=' + str(page)
html = requests.get(url=url, headers=headers).content
x = bs(html, 'lxml')
# 主要是为了调试看爬取的对不对
# print x.title
# 微博内容全部在 <span class="ctt">
#但是对于某些转发内容来说,转化理由在 <span class="cmt"> 标签下
contents = x.find_all(class_='ctt')
for content in contents:
word = content.get_text()
f.write(word.encode('utf-8'))
page += 1
time.sleep(3)
f.close()
但是这有一个问题,对于长微博来说
页面.png
对于每个 <span class="ctt"> 下都有
a
标签,怎么找到这个标签下的 a
标签从而找到全文href
对我暂时是个问题,希望有朋友看到不吝赐教!