【问题标题】:How do I retrieve the text between those我如何检索这些之间的文本
【发布时间】:2020-08-16 09:16:11
【问题描述】:

https://imgur.com/a/JcTnbiw

如何使用 beautifulsoup 检索突出显示的文本? 一个例子将是最好的答案,谢谢;)

编辑;这是代码

import requests
import pyperclip
from bs4 import BeautifulSoup
import time

url = 'https://sales.elhst.co/'

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"}

site = requests.get(url, headers=headers)
site = str(site)
if site == "<Response [200]>":
    print("Site is up..")

page = requests.get(url, headers=headers)


soup = BeautifulSoup(page.content, 'html.parser')

time.sleep(2)

target = soup.find("pp", id="copies")

print(target)

输出是:

Site is up..
<pp id="copies"></pp>

我想得到这个文本: https://imgur.com/a/JcTnbiw 有什么办法吗?

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:
from lxml import html
import requests

page = requests.get('http://url')
tree = html.fromstring(page.content)

#This will extract the text you need
buyers = tree.xpath('//pp[@id="copies"]/text()')

它应该工作。但我不知道pp 标签。我认为这是一个错误,应该有标签&lt;p&gt;

关于lxmlhere的更多信息。

【讨论】:

    【解决方案2】:

    您在页面上看到的数据是从外部 URL 加载的。您可以尝试使用此脚本打印份数:

    import re
    import json
    import requests
    
    
    url = 'https://sales.elhst.co/socket.io/?EIO=3&transport=polling'
    copies_url = 'https://sales.elhst.co/socket.io/?EIO=3&transport=polling&sid={sid}'
    
    r = requests.get(url).text
    sid = json.loads(re.search(r'(\{".*)', r).group(1))['sid']
    
    r = requests.get(copies_url.format(sid=sid)).text
    copies = json.loads(re.search(r'(\[".*)', r).group(1))[-1]
    
    print(copies)
    

    打印:

    0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多