【问题标题】:Python requests html printed response in arrayPython请求数组中的html打印响应
【发布时间】:2018-10-14 08:55:41
【问题描述】:

我正在尝试检查链接是否包含 http 并打印 URL。

import requests
from requests_html import HTMLSession
import sys

link = "http://www.tvil.me/view/93/4/8/v/%D7%90%D7%99%D7%99_%D7%96%D7%95%D7%9E%D7%91%D7%99_IZombie.html"
enter_episodes = HTMLSession().get(link)
page = enter_episodes.html
s = page.xpath("//*[@class='view-watch-button']/a")
for l in s:
    link = l.links
    if link != "set()":
        print(link)

回复:

{'http://streamcloud.eu/ga4m4hizbrfb/iZombie.S04E08.HDTV.x264-SVA.mkv.html'}
{'http://uptostream.com/77p26f7twwhe'}
set()
{'https://clipwatching.com/aog2ni06rzjt/rrFhepnbFfpt6xg.mkv.html'}
set()
[Finished in 1.7s]

我试图删除set() 响应并仅获取没有{''} 的链接。

【问题讨论】:

  • 在集合中使用pop() 以获得完整结果;你必须在其他地方打印set()

标签: python python-3.x python-requests python-requests-html


【解决方案1】:

你只需要确保集合的长度大于一,然后弹出它:

import requests
from requests_html import HTMLSession
import sys

link = "http://www.tvil.me/view/93/4/8/v/%D7%90%D7%99%D7%99_%D7%96%D7%95%D7%9E%D7%91%D7%99_IZombie.html"
enter_episodes = HTMLSession().get(link)
page = enter_episodes.html
s = page.xpath("//*[@class='view-watch-button']/a")
for l in s:
    link = l.links
    if len(link) > 0: # make sure it has a value
        print(link.pop()) # get the last value (in your case, the only one)

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2017-07-03
    • 2015-01-26
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多