gpt4 book ai didi

python - 如何从Python表中抓取特定的tr或td

转载 作者:行者123 更新时间:2023-12-01 00:36:22 25 4
gpt4 key购买 nike

我想抓取该网站的名字和姓氏,以便在自动浏览器输入中使用它。

from lxml import html
import requests

page = requests.get('https://www.getnewidentity.com/uk-identity-generator.php')
tree = html.fromstring(page.content)


firstname = tree.xpath('//*[@id="reslist"]/tbody/tr[3]/td[2]/text()')

lastname = tree.xpath('//*[@id="reslist"]/tbody/tr[4]/td[2]/text()')

print ('FirstName: ', firstname)
print ('LastName: ', lastname)

input("close")

网站是这个https://www.getnewidentity.com/uk-identity-generator.php

<table class="table table-bordered table-striped" id="reslist"><thead><tr><th colspan="2" class="bg-primary">General Information</th></tr></thead><tbody><tr><td style="width:150px;">Name</td><td><b>Kamila Harmon</b></td></tr>
<tr><td>Gender</td><td>Female</td></tr>
<tr><td>First Name</td><td>Kamila</td></tr>
<tr><td>Last Name</td><td>Harmon</td></tr>
<tr><td>Birthday</td><td>12/26/1989</td></tr>

最佳答案

  • find_all() -返回元素的集合。
  • strip() - Python 内置函数用于删除字符串中所有前导和尾随空格。

例如

from bs4 import BeautifulSoup
import requests

request = requests.post('https://www.getnewidentity.com/data/uk-identity-generator.php'
,data={"num":"undefine","add":"address","unique":"true"})

soup = BeautifulSoup(request.content,'lxml')
td = soup.find_all("td")
data = {}
for x in range(0,len(td)-1,2):
data[td[x].text.strip()] = td[x+1].text.strip()

print(data)

O/P:

{'Name': 'Jayda Key', 'Gender': 'Female', 'First Name': 'Jayda', 'Last Name': 'Key', 
'Birthday': '55', 'NINO': 'EB 29 38 84 B', 'Address': 'Flat 31l\nMartin Walk, Leoberg, S81
0HT', 'Street Address': 'Flat 31l\nMartin Walk', 'State': 'Leoberg', 'Zip Code': 'S81 0HT',
'Phone': '+44(0)9487 957056', 'Credit Card Type': 'MasterCard', 'Credit Card Number':
'5246585772859818', 'CVV': '899', 'Expires': '02/2022', 'Username': 'twinhero', 'Email':
'Gamestomper@gmail.com', 'Password': 'Go7ByznZ', 'User Agent': 'Mozilla/5.0 (Macintosh;
Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2
Safari/601.7.7', 'Height': '1.85m (6.17ft)', 'Weight': '75.22kg (158.31pounds)',
'Blood type': 'O−'}

关于python - 如何从Python表中抓取特定的tr或td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57726629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com