python爬取iframe嵌套页面,Python BeautifulSoup-使用来自给定URL的iframe抓取多个网页

We have this code (thanks to Cody and Alex Tereshenkov):

import pandas as pd

import requests

from bs4 import BeautifulSoup

pd.set_option('display.width', 1000)

pd.set_option('display.max_columns', 50)

url = "https://www.aliexpress.com/store/feedback-score/1665279.html"

s = requests.Session()

r = s.get(url)

soup = BeautifulSoup(r.content, "html.parser")

iframe_src = soup.select_one("#detail-displayer").attrs["src"]

r = s.get(f"https:{iframe_src}")

soup = BeautifulSoup(r.content, "html.parser")

rows = []

for row in soup.select(".history-tb tr"):

#print("\t".join([e.text for e in row.select("th, td")]))

rows.append([e.text for e in row.select("th

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Python 和 Beautiful Soup 进行多页数据爬取的一般步骤如下: 1. 导入所需的库: ```python import requests from bs4 import BeautifulSoup ``` 2. 构造爬取多个页面URL 列表: ```python url_list = ['http://example.com/page1', 'http://example.com/page2', 'http://example.com/page3'] ``` 3. 循环遍历 URL 列表,逐个请求页面并解析数据: ```python for url in url_list: # 发送 GET 请求并获取响应内容 response = requests.get(url) # 创建 Beautiful Soup 对象并指定解析器 soup = BeautifulSoup(response.content, 'html.parser') # 解析数据 # ... ``` 4. 在循环中解析数据,可以使用 Beautiful Soup 提供的方法,例如 find_all() 和 select()。下面是一个示例代码: ```python for url in url_list: # 发送 GET 请求并获取响应内容 response = requests.get(url) # 创建 Beautiful Soup 对象并指定解析器 soup = BeautifulSoup(response.content, 'html.parser') # 解析数据 items = soup.select('.item') for item in items: title = item.select_one('.title').text.strip() price = item.select_one('.price').text.strip() # 处理数据 # ... ``` 这里使用了 CSS 选择器来定位需要的元素,具体可以参考 Beautiful Soup 的文档。 5. 处理数据。在循环中处理数据的方法取决于具体情况,可以将数据保存到文件或数据库中,也可以将数据放入列表或字典中进行进一步处理。 完整的代码示例: ```python import requests from bs4 import BeautifulSoup url_list = ['http://example.com/page1', 'http://example.com/page2', 'http://example.com/page3'] for url in url_list: # 发送 GET 请求并获取响应内容 response = requests.get(url) # 创建 Beautiful Soup 对象并指定解析器 soup = BeautifulSoup(response.content, 'html.parser') # 解析数据 items = soup.select('.item') for item in items: title = item.select_one('.title').text.strip() price = item.select_one('.price').text.strip() # 处理数据 # ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值