好像你们特别喜欢美女嗷所以做了一个美女写真爬虫
本帖最后由 cdsgg 于 2021-3-24 14:20 编辑import requests
from bs4 import BeautifulSoup
def getUrl():
headers = {
"referer": "https://www.baidu.com/link?url=GgeDs1AzZQg7jAVgRWaaQVemNzUYNHFpN-PiFmUIyUHeaMQeoPb14_8g1oL7GmBggm70mm4e2EkM5F7lmQMUVq&wd=&eqid=fdcd73c000068a5900000004605ac987",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
}
url = 'https://www.tupianzj.com/meinv/mm/nkmv/'#其实这里改改 可以爬取更多分类 图片下面有显示
urllist = []
H = requests.get(url, headers=headers).text
soup = BeautifulSoup(H, 'lxml').select('ul.d1.ico3>li>a')
ullist =
for i in ullist:
if '/meinv/' in i:
urllist.append(i)
return urllist
def getpage(imglist):
for l in imglist:
headers = {
"referer": "https://www.baidu.com/link?url=GgeDs1AzZQg7jAVgRWaaQVemNzUYNHFpN-PiFmUIyUHeaMQeoPb14_8g1oL7GmBggm70mm4e2EkM5F7lmQMUVq&wd=&eqid=fdcd73c000068a5900000004605ac987",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
}
R = requests.get(f"https://www.tupianzj.com{l}", headers=headers).content.decode('gbk')
imgsoup = BeautifulSoup(R, 'lxml').select('.pages > ul:nth-child(1) > li:nth-last-child(2)>a').text
for i in range(1, int(imgsoup) + 1):
if i == 1:
imgurl = BeautifulSoup(R, 'lxml').select('#bigpicimg').get('src')
tittle = BeautifulSoup(R, 'lxml').select('div.list_con:nth-child(3) > h1:nth-child(1)').text
with open(tittle + '.' + imgurl.split('.')[-1], 'wb') as f:
f.write(requests.get(imgurl, headers=headers).content)
f.close()
print(tittle + " 保存成功!")
else:
imgurl = 'https://www.tupianzj.com' + l.replace('.html', '') + f'_{i}.html'
req=requests.get(imgurl,headers=headers).content.decode('gbk')
u=BeautifulSoup(req, 'lxml').select('#bigpicimg').get('src')
tittle = BeautifulSoup(req, 'lxml').select('div.list_con:nth-child(3) > h1:nth-child(1)').text
with open(tittle + '.' + u.split('.')[-1], 'wb') as f:
f.write(requests.get(u, headers=headers).content)
f.close()
print(tittle+" 保存成功!")
if __name__ == '__main__':
listur = getUrl()
getpage(listur)
没啥意思(提裤子)/doge 我把nkmn改成rentiyishyu,为什么就报错
Traceback (most recent call last):
File "D:/demo/main.py", line 55, in <module>
getpage(listur)
File "D:/demo/main.py", line 33, in getpage
imgsoup = BeautifulSoup(R, 'lxml').select('.pages > ul:nth-child(1) > li:nth-last-child(2)>a').text
IndexError: list index out of range
好刺激,身体一天不如一天{:301_997:} 好家伙,能不能给J儿放天假????{:301_995:} 新手怎么使用? 不会用 有没有人能教教我 进来没别的意思,单纯学习下代码的写法{:301_1004:} 新人表示喜欢,但是不会用,求出教程{:301_995:} 一般【提裤】{:301_995:} 太有意思了,我冲了