该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import requests
from lxml import etree
import json
class Tieba():
def __init__(self,name):
self.name = name
self.header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko)"}
def get_url_list(self):
url = "https://tieba.baidu.com/f?kw="+self.name+"&ie=utf-8&pn={}&"
url_list = []
for i in range(5):
url_list.append(url.format(i*50))
return url_list
def parse_url(self):
url_list = self.get_url_list()
for url in url_list:
response = requests.get(url,headers=self.header)
html = response.text
html = etree.HTML(html)
xml = html.xpath("//div[@class = 't_con cleafix']//div[@class = 'threadlist_lz clearfix']/div/a/@href")
return xml
def get_img(self):
link_list = self.parse_url()
for link in link_list:
url = "https://tieba.baidu.com" + link
html = requests.get(url,headers= self.header).text
html = etree.HTML(html)
list = html.xpath('//div/img[@class="BDE_Image"]/@src')
print(list)
return list
def save(self):
list = self.get_img()
with open('test.txt','w') as f:
f.write(json.dumps(list, ensure_ascii=False, indent=2))
if __name__ == '__main__':
tieba=Tieba("六学")
tieba.save()