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

标签 python beautifulsoup screen-scraping

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

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/

相关文章:

python - 如何识别 xarray 中的时间、lon 和 lat 坐标?

python - 如何为 __getitem__() 调用执行 assert_has_calls?

python - 用漂亮的汤 4 python 抓取网页

python - BeautifulSoup:如何解析表中未标识的 TD 列表

Haskell 通过浏览器获取 URL

python - 如何将新关键字放入 'keyword' 模块?

python - TKInter Python 检查按钮状态

python - 为什么 find_all BeautifulSoup4 函数没有返回任何内容?

python - 如何使用 'contents'来抓取我想要的值?

ruby-on-rails - Nokogiri:如何通过 id 查找 div 并查看它包含哪些文本?