【问题标题】:bs4 loop stop on the Mid of the links why?bs4 loop stop on the Mid of the links 为什么?
【发布时间】:2021-05-22 10:34:23
【问题描述】:

我有一个 python 脚本 bs4 scraper,它从我从文本文件中提供给它的 web 链接中抓取 mp4 播放器链接。循环正常运行,但在其中一个链接处停止。就像我在 6 个链接下面传递它一样,它们运行 3 个然后停止而没有任何错误。

这些是链接:link1link2link3link4link5link6

这是我的代码:

from bs4 import BeautifulSoup
import requests

text_file = open(r"Python\Anime_Episodes_Links\Links2.txt","r",encoding="utf-8")


num_link = 1

for a in text_file:

    source = requests.get(text_file.readline())

    soup = BeautifulSoup(source.text, 'lxml')


    contener = soup.find('div', {'id': 'episode-watch-content'})

    server_links = contener.find('ul', {'id': 'episode-servers'})

    epi_link = server_links.find('a')['data-ep-url']

    print(str(num_link) + '-' + str(epi_link))
    num_link = num_link + 1


print('DONE')

需要帮助来了解我的代码中的问题所在,即它没有针对所有 6 个问题完成。

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    我稍微修改了你的代码,希望这是你想要的结果:

    from bs4 import BeautifulSoup
    import requests
    
    text_file = open(r"a.txt", "r")
    
    num_link = 1
    
    for line in text_file.readlines():
       source = requests.get(line)
       soup = BeautifulSoup(source.text, 'lxml')
       contener = soup.find('div', {'id': 'episode-watch-content'})
       server_links = contener.find('ul', {'id': 'episode-servers'})
       epi_link = server_links.find('a')['data-ep-url']
       print(str(num_link) + ' - ' + str(epi_link))
       num_link = num_link + 1
    
    print('DONE')
    

    当我运行程序时,它设法打印了 6 个链接:

    1 - https://moshahda.online/embed-fk3xtl0vsec3.html
    2 - https://moshahda.online/embed-awer88vmgnx7.html
    3 - https://moshahda.online/embed-50hnaf9p7qyq.html
    4 - https://moshahda.online/embed-2gd52ruefqyt.html
    5 - https://moshahda.online/embed-sz2mcl9r1nhz.html
    6 - https://moshahda.online/embed-48u0cit7d0ef.html
    DONE
    

    【讨论】:

    • Thankkkkk youuuuuuuuu 我的朋友,它现在运行得更好了谢谢你这么马赫
    猜你喜欢
    • 2022-12-02
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2022-12-26
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多