DrissionPage使用
前言
结合了Selenium和Playwright两种
一、DrissionPage
1.官方链接
https://www.drissionpage.cn/官方超链接:
根据官方文档学习使用即可
2.代码示例
页面翻转和获取页数
def reset_to_first_page(page):
"""强制将页面重置到第一页"""
try:
home_button = page.ele('@id=listFormB:list:itemScroller')
if home_button:
button = home_button.ele('@text()=首页')
if button:
button.click()
wait_for_element(page, '@id=listFormB:list', timeout=10)
except Exception as e:
print(f"重置到第一页时出错: {e}")
def get_total_pages(page):
"""获取总页数"""
try:
pagination_info = page.ele('@class=rich-datascr-info')
if pagination_info:
text = pagination_info.text
if '共' in text and '页' in text:
return int(text.split('共')[1].split('页')[0].strip())
return 0 # 默认值
except Exception as e:
print(f"获取总页数时出错: {e}")
return 0
提取数据包(前面定义过和一直在监听,所以这里没有显示,具体代码我会上传资源(可能))
for item in page.listen.steps():
response_data = item.response.body
soup = BeautifulSoup(response_data, 'html.parser')
# 查找表格
table = soup.find('table', {'id': 'listFormB:list'})
if table:
rows = table.find_all('tr', class_=lambda x: x in [
'rich-table-row oddColumn',
'rich-table-row evenColumn',
'rich-table-row rich-table-firstrow oddColumn'
])
for row in rows:
cells = row.find_all('td', class_='rich-table-cell')
row_data = [cell.get_text(strip=True) for cell in cells]
results.append(row_data)
3.如何获取数据包端口
点击网络之后-重新加载-请求URL就是端口了
这里借用了百度做案例,侵权即删
该处使用的url网络请求的数据。
总结
侵权即删!!!!!!!!!!