关于pixiv网站爬取的问题
网上看了很多别人写的程序要么用不了
要么出错找不到原因
要么就是不用登录的,不登录爬取的图片又有限制
于是就想自己写,但是登录时遇到很大的问题,我把登录需要的参数传过去了,但是后面requests.text传回来的网页源码显示我没登录
这是我写的代码,卡在了登录这一步好久了,希望看到的大神能指导一下(网址是https://accounts.pixiv.net/login?lang=zh,需要翻墙才能登录)
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlencode
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def login():
headers = {
'User-Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/83.0.4103.97Safari/537.36',
# 'referer': 'https://accounts.pixiv.net/login?return_to=https%3A%2F%2Fwww.pixiv.net%2F&lang=zh&source=pc&view_type=page'
'Referer': 'https://accounts.pixiv.net/login?lang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index'
}
# session = requests.session()
# response = session.get('https://accounts.pixiv.net/login?lang=zh',headers=headers,verify=False)
response = requests.get('https://accounts.pixiv.net/login?lang=zh',headers=headers,verify=False)
print(response.status_code)
bs = BeautifulSoup(response.text,'html.parser')
post_key = bs.find('div',id='old-login').find('input',type='hidden')['value']
print(post_key)
data = {
'post_key': post_key,
'captcha':'',
'g_recaptcha_response':'',
'ref':'',
'return_to': 'https://www.pixiv.net/',
'source': 'pc',
'pixiv_id': '@qq.com',
'password': ''
}
res = requests.post('https://accounts.pixiv.net/api/login?lang=zh',data=data,headers=headers,verify=False)
print(res.cookies)
print(type(res.cookies))
return res.cookies
def get_id():
response_list = []
cookies = login()
headers2 = {
'User-Agent': 'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/83.0.4103.97Safari/537.36',
'Accept-Language': 'zh-CN,zh',
'Referer':'https://accounts.pixiv.net/login?return_to=https%3A%2F%2Fwww.pixiv.net%2F&lang=zh&source=pc&view_type=page'
}
# url = 'https://www.pixiv.net/ranking.php?mode=daily_r18&p=1&format=json'
data = {
'mode':'daily_r18'
}
url = 'https://www.pixiv.net/ranking.php?'+urlencode(data)
print(url)
response = requests.get(url,headers=headers2,data=data,cookies=cookies,verify=False)
print(response.text)
# global false, null, true
# false = 'False'
# null = 'None'
# true = 'True'
res = response.json()
print(res)
list_id = res['contents']
for i in list_id:
pic_id = i['illust_id']
pic_name = i['title']
response_list.append()
return response_list
def main():
pic_id = get_id()
main()
要是能够解决登录问题,后续的爬取应该没什么问题了 上面的data里的post_key参数可以在登录页面的网页源码里直接获取,python小白,好多东西不会,请各位大佬指点一下 q1274701999 发表于 2020-6-9 19:08
要是能够解决登录问题,后续的爬取应该没什么问题了
被你这个帖子提醒....我突然想起我都快忘记我P站账号了 q1274701999 发表于 2020-6-9 19:11
上面的data里的post_key参数可以在登录页面的网页源码里直接获取,python小白,好多东西不会,请各位大佬指 ...
用无头浏览器 P站免爬墙第三方工具登录方法:https://www.sohu.com/a/397863419_197722?spm=smpc.author.fd-d.2.1590843479898dQ57VKi
各位大佬帮忙看一下啊 wp231957 发表于 2020-6-9 20:11
用无头浏览器
跟浏览器没关系吧,我用的谷歌浏览器 经过我自己的一番捣鼓,最终登陆成功了!!
应该是我cookies的提取出了问题
我看了别人的代码,用chrome的一个提取cookies的插件,提取出页面的cookies,然后保存到本地
访问网页的时候读取里面所有的cookies
for i in range(len(cookie)):
if cookie["domain"] == '.pixiv.net':
cookies_json["name"]] = cookie["value"]
取里面的某一段cookie
把cookies添加到session.cookies里
requests.utils.add_dict_to_cookiejar(session.cookies,cookies)
其实这一段我也不明白什么意思
然后就可以模拟登录网页了 用selenium
页:
[1]