|
60鱼币
我利用requests模块的session方法进行网站的登录 并检索文献 代码如下
- import requests
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
- }
- user_data = {
- 'username':'xxx',
- 'password':'xxx'
- }
- login_url = 'https://sci-hub.org.cn/loginhub'
- paper_data = {
- "q": "(16) Schmidt-Rohr, K.; Chen, Q. Parallel Cylindrical Water Nanochannels in Nafion Fuel-Cell Membranes. Nat. Mater. 2008, 7, 75−83.",
- }#这个暂时没有用
- search_url_0 = 'https://sci-hub.org.cn/scholar?hl=zh-TW&as_sdt=0%2C5&q='
- def replace_string(string):
- '''这个函数是将输入的参考文献格式转换成url文献检索网址的格式'''
- replace_str = [' ', ',', ';', '(', ')']
- changed_str = ['+', '%2C', '%3B', '%28', '%29']
- for i in range(len(replace_str)):
- string = string.replace(replace_str[i], changed_str[i])
- return string
- session = requests.session()
- paper_name = input("请输入你要检索的参考文献:")
- search_url_1 = replace_string(paper_name)
- search_url = search_url_0 + search_url_1
- resp0 = session.post(login_url, data=user_data, headers=headers)#登录网站
- resp = session.get(search_url, headers=headers)#进行检索
- print(resp.text)
- resp.close()
- resp_0.close()
复制代码
但是出现了问题 首先无论是resp0还是resp进行raise_for_status()检查返回都是None
然后是resp.text返回如下,这并不是我想要的网页代码,我保存为html文件(代码如下)用网页打开也是一片空白,想请教一下是哪里出现了问题
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0"/>
- <meta http-equiv="Connection" content="Close"/>
- <script type="text/javascript">
- function AutoJump(){
- var now = new Date();
- var time = now.getTime();
- var refresh = window.localStorage.getItem('refresh');
- if (refresh===null || (time-refresh)>60*1000){
- window.localStorage.setItem('refresh', time);
- window.location.reload();
- }
- }
- </script>
- <script>
- var _hmt = _hmt || [];
- (function() {
- var hm = document.createElement("script");
- hm.src = "https://hm.baidu.com/hm.js?dad9beaf45839ce372642d1247c47d5d";
- var s = document.getElementsByTagName("script")[0];
- s.parentNode.insertBefore(hm, s);
- var now = new Date();
- var time = now.getTime();
- time += 600 * 1000;
- now.setTime(time);
- document.cookie="Hm_vpk_ffd1e784a7aa6460b13e495f35cc5dd760d6d9f35b866c81a69aec28=1648711710;expires="+now.toGMTString()+";path=/;";
- })();
- </script>
- <script>setTimeout("AutoJump()", 20);</script>
- </head>
- </html>
复制代码
使用resp.text返回的内容确实是正确的。
显示的内容跟网页源代码不一至,是因为网页源代码是经过处理后的内容(所谓的加密,反爬虫)。
可以尝试使用别的模块,如 selenium+phantomjs模拟浏览器
|
最佳答案
查看完整内容
使用resp.text返回的内容确实是正确的。
显示的内容跟网页源代码不一至,是因为网页源代码是经过处理后的内容(所谓的加密,反爬虫)。
可以尝试使用别的模块,如 selenium+phantomjs模拟浏览器
|