|
楼主 |
发表于 2020-7-16 19:34:55
|
显示全部楼层
import requests
from lxml import etree
class TiebaSpider:
def __init__(self,tieba_name):
self.start_url='https://tieba.baidu.com/f?kw='+tieba_name+'&mo_device=1&pn=30&'
self.headers={
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36',
'Cookie':'BAIDUID=3910AAC3648F83F7C91FF62BFEEA3A5B:FG=1; PSTM=1594791849; BIDUPSID=A2C9C50B4451698E8184A5127F79101E; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; TIEBA_USERTYPE=6a1d9b62b655485ece22e331; TIEBAUID=cb23caae14130a0d384a57f1; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1594843199; st_key_id=17; wise_device=0; mo_originid=2; IS_NEW_USER=c6ccba2c6228269eac804531; BAIDU_WISE_UID=wapp_1594843363615_668; USER_JUMP=-1; CLIENTHEIGHT=854; CLIENTWIDTH=505; SEENKW=%E6%9D%8E%E6%AF%85%23%C0%EE%D2%E3; LASW=438; tb_as_data=4456f8ecf42b3af3a51ef6ea935a01f27d2a7a870cccced869901a220fce8c7c96cadfab4b8a09ab92f3b8b2ee59517fc9171f24849c9ac8c876069a8f0fce82e8316b8b7aa12a7d91f45f4bdabb157527bdbea2a9a9c34951b455c83aabb44dc5eb764996c71d96871c92136257eca5; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1594843579; st_data=d1c224669d51a4f51e54c234cfd7fe5dc27d3abd9093bfde2c26d005e01a4983a767a2283682899a336c9984c0ef2a7040e18c05fe4e46f1af81e114fe504bcdd9dbc90a669bca7e765bffc8bb9e82758f3fd788ae2fe18bac7df53c4ccc45ac375988682f4e7ecbc4780269fe01be9b745511ca7bcce149c6fc7ea4c104624a; st_sign=814136cd'
}
def parse_url(self,url):
response=requests.get(url,headers=self.headers,verify=False)
rel=response.content.decode()
with open('baidutieba.html','w',encoding='utf-8') as f:
f.write(rel)
print('ok')
return rel
def get_content_list(self,html_str):
html=etree.HTML(html_str)
def run(self):
#1 start_url
#2 发送请求 获取响应
html_str=self.parse_url(self.start_url)
#3 提取数据 提取下一页的URL地址
#3.1 提取列表中的url地址和标题
#3.2 请求别表中的url地址 获取详情页的第一页
#3.3 提取详情页第一页的图片,提取下一页的url地址
#3.4 请求下一页 进入循环 3.2---3.4
# 保存数据
# 请求下一页的url地址 进入循环2-5步
if __name__ == "__main__":
baidu=TiebaSpider('李毅')
baidu.run() |
|