【问题标题】:Python BeautifulSoup Parsing Script TagsPython BeautifulSoup 解析脚本标签
【发布时间】:2018-11-21 01:08:54
【问题描述】:

我正在尝试解析脚本标签中的内容以提取某些数据。以下代码使用有效的 Xbox 真实帐户。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
import time
from bs4 import BeautifulSoup
import json
import re

email = 'email'
password = 'password'

driver = webdriver.Chrome()

driver.get(r'https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&rver=6.7.6643.0&wp=MBI_SSL&wreply=https:%2f%2faccount.xbox.com%2fen-us%2faccountcreation%3freturnUrl%3dhttps:%252f%252fwww.xbox.com:443%252fen-US%252f%26pcexp%3dtrue%26uictx%3dme%26rtc%3d1&lc=1033&id=292543&aadredir=1')
time.sleep(3)
driver.find_element_by_xpath(""" //*[@id="i0116"] """).send_keys(email)
time.sleep(5)
driver.find_element_by_xpath(""" //*[@id="idSIButton9"] """).click()
time.sleep(5)
driver.find_element_by_xpath(""" //*[@id="i0118"] """).send_keys(password)
time.sleep(5)
driver.find_element_by_xpath(""" //*[@id="idSIButton9"] """).click()
time.sleep(5)
driver.get(r'https://account.xbox.com/en-us/Friends?xr=mebarnav&rtc=1')
print('Grabbing Cookies')
time.sleep(5)


headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}

s = requests.Session()
s.headers.update(headers)

for cookie in driver.get_cookies():
    c = {cookie['name'] : cookie['value']}
    s.cookies.update(c)

#s.get('https://account.xbox.com/en-us/Friends?xr=mebarnav&rtc=1')


soup = BeautifulSoup(s.get('https://account.xbox.com/en-us/Profile?xr=mebarnav&activetab=tertiary:friendsTab&rtc=1').content, 'html.parser')

text = str(soup.find_all('script')[13])

value = re.findall(r'DisplayName', text)

print(value)

我正在尝试访问每个“DisplayName”之后的某些数据,但我没有这样做,因为我只是得到“DisplayName”而不是它的值。如果您需要更好的主意,可以打印“text”变量并搜索“DisplayName”。感谢所有提前回复的人。

【问题讨论】:

  • 复制显示名称代码并粘贴到此处。您将获得带有 re 的显示名称,但后面没有任何内容。发送代码,我会帮你解决这个问题。

标签: python regex selenium beautifulsoup python-requests


【解决方案1】:

所以你没有得到任何东西的原因是因为你告诉re 搜索确切的短语。您不是在告诉它要获取更多字符以及在哪里停止。在下面的示例中,我使用单引号,但可以针对双引号调整代码。然后我让re 找到 DisplayName,但 .* 找到它后面的字符,但停在单引号 ' 处。然后,它只是替换你不想要的东西。

import re

url = "DisplayName='PoppaBear4'"

info = re.findall(r"DisplayName=.*'", url)
print(str(info).replace("DisplayName='",'').replace("'","").replace('["','').replace('"]',''))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-22
    • 2019-03-06
    • 2017-12-11
    • 2021-03-20
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2016-05-06
    相关资源
    最近更新 更多