1.引入正则:
使用正则的可以把获取到的整个页面里指定的需要的部分匹配出来
引入模块:
import re
2.实践:
import requests
import re
if __name__ == "__main__":
#UA伪装:将对应的User-Agent封装到一个字典中
headers = {
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36 Edg/121.0.0.0'
}
url = 'https://travel.state.gov/content/travel/en/legal/visa-law0/visa-bulletin/2024/visa-bulletin-for-march-2024.html'
#https://travel.state.gov
#https://travel.state.gov/content/travel/en/legal/visa-law0/visa-bulletin.html
#https://travel.state.gov/content/travel/en/legal/visa-law0/visa-bulletin/2024/visa-bulletin-for-march-2024.html
#-->地址变化,发起的不是一个ajax请求
#get方法会返回一个响应对象
response = requests.get(url=url,headers=headers)
#text返回的是字符串形式的响应数据
page_text=response.text
#print(page_text)
ex='<table\s+bord

本文介绍了如何使用Python的requests库和正则表达式,通过伪装User-Agent抓取网页并提取特定表格内容,解决正则匹配HTML表格外层结构的问题,包括处理空格和HTML属性顺序的灵活性。
最低0.47元/天 解锁文章
3392

被折叠的 条评论
为什么被折叠?



