Python 如何在抓取网页时搜索部分id?

Python 如何在抓取网页时搜索部分id?,python,web,web-scraping,beautifulsoup,Python,Web,Web Scraping,Beautifulsoup,我正在学习pythonwebscraping,我正在测试它,到目前为止,它只提供了我需要的一条记录 在检查我正在测试的id时,在前面添加了一些字符。e、 g: id="List_1__firstName" 因此,我希望使用部分id来获取记录,更像%%\u firstName _firstName 我该怎么办?这是我当前的代码: import requests from bs4 import BeautifulSoup url = 'https://****.co**/

我正在学习pythonwebscraping,我正在测试它,到目前为止,它只提供了我需要的一条记录

在检查我正在测试的id时,在前面添加了一些字符。e、 g:

 id="List_1__firstName"
因此,我希望使用部分id来获取记录,更像
%%\u firstName

_firstName
我该怎么办?这是我当前的代码:

import requests
from bs4 import BeautifulSoup

url = 'https://****.co**/'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
Name = soup.find(id='List_l0_l00_firstName').get_text()
print(Name)

也许下面的问题对你有帮助

您可以使用
find_all

重新导入
soup.find_all(id=re.compile(''u firstName$'))
补充意见 这是我的本地测试脚本和结果,在下面的评论中提到


真奇怪。。。事实上,我已经在我的本地测试过了,并且成功了。刚才我在回答中添加了控制台输出、python脚本和html文件

输出 python脚本(main.py)
导入bs4
进口稀土
soup=bs4.BeautifulSoup(打开('index.html'),'html.parser')
elements=soup.find_all(id=re.compile(“'u firstName$”)
对于el in元件:
打印(el.get_text())
html文件(index.html)

测试
福
酒吧
巴兹
我的环境
感谢这个指针,我最终使用了``Name=soup.findAll('span',id=re.compile('u firstName'))``它给了我所有带有数据的span标记,我只想得到该span或div的数据。最初使用get_text(),我只能得到一条记录。我想你可以迭代结果并调用
get_text()
对于每个元素。它给我这个错误attributeError:'re.Pattern'对象没有属性'get\u text',这很奇怪。。。事实上,我已经在我的本地测试过了,并且成功了。刚才我在回答中添加了控制台输出、python脚本和html文件。
$ ls 
index.html  main.py

$ python3 main.py
foo
bar
baz
$ python --version
Python 3.7.6

$ pip show beautifulsoup4
Name: beautifulsoup4
Version: 4.9.3
Summary: Screen-scraping library
Home-page: http://www.crummy.com/software/BeautifulSoup/bs4/
Author: Leonard Richardson
Author-email: leonardr@segfault.org
License: MIT
Location: /Users/ntb/envs/bs/lib/python3.7/site-packages
Requires: soupsieve
Required-by: