使用 python html 错误抓取网络数据

crawling web data using python html error

我想使用 python 来抓取数据 我试过再试一次 但它没有用 我找不到代码的错误 我写了这样的代码:

import re
import requests
from bs4 import BeautifulSoup

url='http://news.naver.com/main/ranking/read.nhn?mid=etc&sid1=111&rankingType=popular_week&oid=277&aid=0003773756&date=20160622&type=1&rankingSectionId=102&rankingSeq=1'
html=requests.get(url)
#print(html.text)
a=html.text
bs=BeautifulSoup(a,'html.parser')
print(bs)
print(bs.find('span',attrs={"class" : "u_cbox_contents"}))

我想抓取新闻中的回复数据

如你所见,我试着烤这个:

span, class="u_cbox_contents" in bs

但 python 只说“None”

None

所以我使用函数 print(bs)

检查 bs

然后我检查 bs 变量的内容

但是没有跨度,class="u_cbox_contents"

为什么会这样?

真不知道为什么

请帮帮我

感谢阅读。

请求将获取 URL 的内容,但不会执行任何 JavaScript。

我用 cURL 执行了相同的提取,但在 HTML 代码中找不到任何 u_cbox_contents 的出现。很可能,它是使用 JavaScript 注入的,这解释了为什么 BeautifulSoup 找不到它。

如果您需要在 "normal" 浏览器中呈现的页面代码,您可以尝试 Selenium. Also have a look at this SO 问题。