Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 指定元素和类名后,BeautifulSoup不以任何内容为目标_Python_Python 3.x_Beautifulsoup_Lxml - Fatal编程技术网

Python 指定元素和类名后,BeautifulSoup不以任何内容为目标

Python 指定元素和类名后,BeautifulSoup不以任何内容为目标,python,python-3.x,beautifulsoup,lxml,Python,Python 3.x,Beautifulsoup,Lxml,我正在努力清理这个网站。特别是有线电视收视率最高的韩剧。这就是inspect元素的外观 这是我的密码 import requests from bs4 import BeautifulSoup url = 'https://en.wikipedia.org/wiki/Korean_drama' response = requests.get(url) soup = BeautifulSoup(response.text, 'lxml') kdramas = soup.find_all(

我正在努力清理这个网站。特别是有线电视收视率最高的韩剧。这就是inspect元素的外观

这是我的密码

import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/wiki/Korean_drama'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
kdramas = soup.find_all(
    'table', class_="wikitable sortable jquery-tablesorter")
print(kdramas)
for kdrama in kdramas:
    print(kdrama.text)
这就是我运行代码时发生的情况

admins-MBP:~ admin$ python3 kdramas.py
[]

我认为指定的代码没有问题


我的建议是尝试层次结构中更高的其他标记并观察输出。不同的站点有不同的标记和类,因此没有一种“一刀切”的解决方案。可以先尝试层次结构中更高的div标记。

我认为可以动态添加
jquery tablesorter
类,这就是为什么BeautifulSoup无法读取它的原因

我的建议是使用引入表的
h3
标记,然后深入挖掘DOM中的第一个表对象

比如:

# h3 tag name is actually in a <span> inside the h3 element
table_lead_in = soup.find('span', id="List_of_highest-rated_Korean_dramas_in_public_broadcast")

for drama_table in table_lead_in.find_next('tbody'):
    for tr in drama_table.find_all_next('tr'):
        rank = tr.find('td').text
        title = tr.find('a').text
        print(f"Title: {title} ** Rank: {rank}")

(注意:在
find()
调用中有一些惰性假设,但出于演示目的,这应该足够了。)

wikitable sortable jquery tablesorter
有时被命名为
wikitable sortable
。 您可以使用CSS选择器选择以
wikitable sortable
开头的类,这两种情况都适用:

import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/wiki/Korean_drama'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
kdramas = soup.select_one('table[class^="wikitable sortable"]:nth-of-type(2)')

for row in kdramas.select('tr'):
    data = [td.get_text(strip=True) for td in row.select('td')]
    print(' '.join('{: <30}'.format(d) for d in data))
导入请求
从bs4导入BeautifulSoup
url='1〕https://en.wikipedia.org/wiki/Korean_drama'
response=requests.get(url)
soup=BeautifulSoup(response.text,“lxml”)
kdramas=soup。选择一个('table[class^=“wikitable sortable]”:类型(2)的第n个)
对于kdramas.select('tr')中的行:
数据=[td.get_text(strip=True)表示第行中的td。选择('td')]

print(“”.join(“”:我认为这更适合作为一个注释-
import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/wiki/Korean_drama'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
kdramas = soup.select_one('table[class^="wikitable sortable"]:nth-of-type(2)')

for row in kdramas.select('tr'):
    data = [td.get_text(strip=True) for td in row.select('td')]
    print(' '.join('{: <30}'.format(d) for d in data))