一只魈咸鱼 发表于 2021-8-11 20:18:59

关于python程序抓到的text内容比抓包工具抓到的text少

程序打印的text(缺少部分)如下:
<ul class="urlli"><div id="stab_1_71"><ul>
    <li><a href="/acg/66382/7.html">42备用</a></li><script>$('#LastJia').css("display","block");$("#LastJia").attr("href","/acg/66382/6.html");$("#posterPica").attr("href","/acg/66382/6.html");var d_banquan='';</script><script>$('#LastJiabaidu').css("display","block");$("#LastJiabaidu").attr("href","/acg/66382/6.html");$("#posterPica").attr("href","/acg/66382/6.html");</script><li><a href="/acg/66382/6.html">第42集</a></li><li><a href="/acg/66382/5.html">第41集</a></li><li><a href="/acg/66382/4.html">第40集</a></li><li><a href="/acg/66382/3.html">第39集</a></li><li><a href="/acg/66382/2.html">第38集</a></li><li><a href="/acg/66382/1.html">第37集</a></li>       
   <!-- <li><a title='第1集' href='/comic1/12637/vod-0-0.html' target="_self">第1集</a></li>-->
    </ul></div></ul>
浏览器抓包工具抓到的:
<ul class="urlli"><div id="stab_1_71"><ul>
    <li><a href="/acg/5659/21.html">42备用</a></li><script>$('#LastJia').css("display","block");$("#LastJia").attr("href","/acg/5659/20.html");$("#posterPica").attr("href","/acg/5659/20.html");var d_banquan='';</script><script>$('#LastJiabaidu').css("display","block");$("#LastJiabaidu").attr("href","/acg/5659/20.html");$("#posterPica").attr("href","/acg/5659/20.html");</script><li><a href="/acg/5659/20.html">第42集</a></li><li><a href="/acg/5659/19.html">第41集</a></li><li><a href="/acg/5659/18.html">第40集</a></li><li><a href="/acg/5659/17.html">第39集</a></li><li><a href="/acg/5659/16.html">第38集</a></li><li><a href="/acg/5659/15.html">第37集</a></li><li><a href="/acg/5659/14.html">第36.5集</a></li><li><a href="/acg/5659/13.html">第36集</a></li><li><a href="/acg/5659/12.html">第35集</a></li><li><a href="/acg/5659/11.html">第34集</a></li><li><a href="/acg/5659/10.html">第33集</a></li><li><a href="/acg/5659/9.html">第32集</a></li><li><a href="/acg/5659/8.html">第31集</a></li><li><a href="/acg/5659/7.html">第30集</a></li><li><a href="/acg/5659/6.html">第29集</a></li><li><a href="/acg/5659/5.html">第28集</a></li><li><a href="/acg/5659/4.html">第27集</a></li><li><a href="/acg/5659/3.html">第26集</a></li><li><a href="/acg/5659/2.html">第25集</a></li><li><a href="/acg/5659/1.html">第24.9集</a></li>       
   <!-- <li><a title='第1集' href='/comic1/12637/vod-0-0.html' target="_self">第1集</a></li>-->
    </ul></div></ul>

网址是:http://www.yhdm6.com/acg/5659/
然后我尝试加入Cookie不知道为什么还报错了==
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 148-163: ordinal not in range(256)

不能懒 发表于 2021-8-11 20:31:41

encode解码问题,

suchocolate 发表于 2021-8-11 21:20:07

浏览器看到的是经过css和js渲染过的代码,和urllib拿到的不一样是正常的。
另外你的报错是解码器用的不对,把你的代码贴出来,报错也贴全。

小王学PY 发表于 2021-8-11 22:02:56

import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
resp = requests.get('http://www.yhdm6.com/acg/5659/', headers=headers)
print(resp.content.decode('utf8'))

没问题啊,我这样能抓全

一只魈咸鱼 发表于 2021-8-12 12:05:32

小王学PY 发表于 2021-8-11 22:02
没问题啊,我这样能抓全

我的天好奇怪,你这个代码我第一次运行抓全了,但是我走了一下我的代码,完事咱俩的都抓不全了:
import requests
searchs = {'关于我转生成为史莱姆的那件事第二季':'http://www.yhdm6.com/acg/66382/'}
for search in searchs.items():
    url = search
    res = requests.get(url)
    res.encoding = 'utf-8'
    print(res.text)


你试试看你的多运行几次然后咱俩交换运行

一只魈咸鱼 发表于 2021-8-12 12:10:47

suchocolate 发表于 2021-8-11 21:20
浏览器看到的是经过css和js渲染过的代码,和urllib拿到的不一样是正常的。
另外你的报错是解码器用的不对 ...

可是我申请的url返回的text里面确实是有那些缺失的信息的,我不是在element看的,是在network中看的,然后关于Cookie的代码:
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',\
         'Cookie':'PHPSESSID=2c9ihsh3epnkmml4734foj0dh0; Hm_lvt_c2c40af8d9153ebf91bb5804a4a5366b=1628734433; HISTORY={video:[{"name":"\u5173\u4E8E\u6211\u8F6C\u751F\u53D8\u6210\u53F2\u83B1\u59C6\u8FD9\u6863\u4E8B\u7B2C\u4E8C\u5B63","link":"http://www.yhdm6.com/acg/5659/","pic":"http://tvax4.sinaimg.cn/large/006sgDEegy1gmddgk8nh8j307i0almyv.jpg"},{"name":"\u6708\u5149\u4E0B\u7684\u5F02\u4E16\u754C\u4E4B\u65C5\u7B2C\u4E00\u5B63","link":"http://www.yhdm6.com/acg/60019/","pic":"http://tvax4.sinaimg.cn/large/006sgDEegy1grxca3z6avj607i0aijss02.jpg"}]}; Hm_lpvt_c2c40af8d9153ebf91bb5804a4a5366b=1628741251'}
resp = requests.get('http://www.yhdm6.com/acg/5659/', headers=headers)
res.encoding = 'utf-8'
print(resp.text)

报错如下:
Traceback (most recent call last):
File "C:\Users\lenovo\Documents\my\Python\mytry.py", line 5, in <module>
    resp = requests.get('http://www.yhdm6.com/acg/5659/', headers=headers)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1271, in _send_request
    self.putheader(hdr, value)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 219, in putheader
    _HTTPConnection.putheader(self, header, *values)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1203, in putheader
    values = one_value.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 115-130: ordinal not in range(256)

nahongyan1997 发表于 2021-8-13 10:22:26

我还以为樱花动漫凉了

一只魈咸鱼 发表于 2021-8-13 11:26:59

nahongyan1997 发表于 2021-8-13 10:22
我还以为樱花动漫凉了

没凉,但是爬它就很邪乎,我直接用字符串爬可以,用列表爬就少数据{:5_94:}

小王学PY 发表于 2021-8-13 11:29:33

一只魈咸鱼 发表于 2021-8-12 12:05
我的天好奇怪,你这个代码我第一次运行抓全了,但是我走了一下我的代码,完事咱俩的都抓不全了:
import ...

我运行了几次都能拿到

小王学PY 发表于 2021-8-13 11:31:32

一只魈咸鱼 发表于 2021-8-12 12:05
我的天好奇怪,你这个代码我第一次运行抓全了,但是我走了一下我的代码,完事咱俩的都抓不全了:
import ...

运行你的的确拿不全

一只魈咸鱼 发表于 2021-8-13 12:06:43

小王学PY 发表于 2021-8-13 11:31
运行你的的确拿不全

这是个什么道理{:5_107:}

isdkz 发表于 2022-1-24 08:50:49

一只魈咸鱼 发表于 2021-8-13 12:06
这是个什么道理

你们俩的url都不一样,一个是66382,一个是5659

一只魈咸鱼 发表于 2022-1-24 09:14:18

isdkz 发表于 2022-1-24 08:50
你们俩的url都不一样,一个是66382,一个是5659

啊这个在另一个帖子解决掉了,不过还是谢谢~

isdkz 发表于 2022-1-24 09:19:45

一只魈咸鱼 发表于 2022-1-24 09:14
啊这个在另一个帖子解决掉了,不过还是谢谢~

好吧
页: [1]
查看完整版本: 关于python程序抓到的text内容比抓包工具抓到的text少