[英]Web crawling - how to select path of element when selecting it doesn't change anything in the URL or xpath?
[英]Doesn't respond when web crawling in python
我想得到一个金十字的清单。 所以
我发现它的 id 值为 #contentarea_right。
沿着子索引,我写了以下代码
a_list = soup.select("#contentarea_right > div#box_type_r > table#trend_tab_1 > tbody > tr > td a")
or
a_list = soup.select("#contentarea_right > div > table#trend_tab_1 > tbody > tr > td a")
or
a_list = soup.select("#contentarea_right > div > tbody > tr > td a")
or
a_list = soup.select("#contentarea_right > div > table > tbody > tr > td a")
但没有任何反应......如何解决?
完整代码
from bs4 import BeautifulSoup
import urllib.request as req
url = "https://finance.naver.com/sise/"
res = req.urlopen(url)
soup = BeautifulSoup(res, "html.parser")
#contentarea_right > div
a_list = soup.select("#contentarea_right > div#box_type_r > table#trend_tab_1 > tbody > tr > td a")
for a in a_list:
name = a.string
print("-",name)
如果我使用较短的选择器
"#contentarea_right #trend_tab_1 a"
然后我得到一些数据
具有不同选择器的示例
from bs4 import BeautifulSoup
import urllib.request as req
url = "https://finance.naver.com/sise/"
res = req.urlopen(url)
soup = BeautifulSoup(res, "html.parser")
rows = soup.select("#contentarea_right #trend_tab_1 tr")
for row in rows:
cols = row.select('td')
print("-", cols[0].text, '|', cols[1].text)
结果
- 청호컴넷 | 3,685
- 한진 | 36,950
- 모다이노칩 | 3,500
- 인터로조 | 28,000
- 티피씨글로벌 | 2,650
- 서연탑메탈 | 2,900
- 아이앤씨 | 5,250
- 바른손 | 2,095
- 한글과컴퓨터 | 10,700
- 금강공업우 | 8,890
box_type_r
是一个类,而不是一个 id,并且该表中没有tbody
。 您在检查器中看到的tbody
可能是由您的网络浏览器自动生成的。 将您的 CSS 选择器更改为以下内容。
soup.select("#contentarea_right > div.box_type_r > table#trend_tab_1 > tr > td a")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.