繁体   English   中英

Python Web Scrape:删除输出中多余的HTML标记。 所有数据均来自页表,get_text和pretiffy不起作用

[英]Python Web Scrape: Remove excess HTML tags in output. All data are from a page table, get_text and pretiffy doesnt work

新手在这里。 我只是靠自己练习python。

我的问题是这样的:

我正在网上抓取这个随机网站,我想获取文件名和文件日期并将它们一起打印。 但是多余的html标签仍然存在,即使我使用get_text和prettify,我也无法摆脱它们。 抱歉,菜鸟有问题。 只是需要帮助。 提前致谢。

这是我的代码:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://1337x.to/popular-tv').text

soup = BeautifulSoup(source, 'lxml')

tvhead = soup.find('tbody')
for tv in tvhead.find_all('tr'):
    filename = tv.find_all('td' , class_='coll-1 name')
    filedate = tv.find_all('td', class_='coll-date')
    print(filename)
    print(filedate)
    print()

输出是这样的:

[<td class="coll-1 name"><a class="icon" href="/sub/41/0/"><i class="flaticon-hd"></i></a><a href="/torrent/3225547/Castle-Rock-S01E10-Romans-720p-HULU-WEB-DL-AAC2-0-H-264-NTb-eztv/">Castle.Rock.S01E10.Romans.720p.HULU.WEB-DL.AAC2.0.H.264-NTb[eztv]</a></td>]
[<td class="coll-date">7am Sep. 12th</td>]

[<td class="coll-1 name"><a class="icon" href="/sub/6/0/"><i class="flaticon-divx"></i></a><a href="/torrent/3225539/Castle-Rock-S01E10-Romans-480p-HULU-WEB-DL-AAC2-0-H-264-BTW-ettv/">Castle.Rock.S01E10.Romans.480p.HULU.WEB-DL.AAC2.0.H.264-BTW[ettv]</a></td>]
[<td class="coll-date">7am Sep. 12th</td>]

[<td class="coll-1 name"><a class="icon" href="/sub/6/0/"><i class="flaticon-divx"></i></a><a href="/torrent/3225653/The-Outpost-S01E08-WEB-h264-TBS-ettv/">The.Outpost.S01E08.WEB.h264-TBS[ettv]</a></td>]
[<td class="coll-date">9am Sep. 12th</td>]

预期的输出应该只是标题和日期,例如:

TV Series title
Date

尝试:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://1337x.to/popular-tv').text

soup = BeautifulSoup(source, 'lxml')

tvhead = soup.find('tbody')
for tv in tvhead.find_all('tr'):
    filename = tv.find_all('td' , class_='coll-1 name')
    filedate = tv.find_all('td', class_='coll-date')
    print(filename[0].text)
    print(filedate[0].text)
    print()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM