import requests from bs4 import BeautifulSoup session = requests.Session() cookie = 'your_cookie_here' session.headers.update({'cookie': cookie}) def get_with_cookie(url): try: resp = session.get(url) return resp.text except requests.exceptions.RequestException as e: print("请求异常:", e) return None # 解析HTML内容,提取商品名称、价格和销量信息 def parse_html(html): try: soup = BeautifulSoup(html, 'html.parser') title = soup.select_one('h3.tb-main-title[data-title]') return title except Exception as e: print("解析异常:", e) return None # 测试接口: url = 'https://item.taobao.com/item.htm?id=699089474285&ali_refid=a3_430673_1006:1102522391:N:%2BAkz14GC%2BXi%2Bma9dw7y9M4p5Yhq5iT8I:f9e826228fbc823934848d20e28dff81&ali_trackid=162_f9e826228fbc823934848d20e28dff81&spm=a2e0b.20350158.31919782.5' resp_text = get_with_cookie(url) if resp_text is not None: title = parse_html(resp_text) if title is not None: # 打印商品信息 print(title.text.strip()) else: print("解析失败") else: print("请求失败")用其他方式帮我解析对应位置的数据
时间: 2023-12-29 17:02:47 浏览: 163
可以使用正则表达式或者XPath来解析HTML内容,以下是两种方法的示例:
1. 使用正则表达式解析商品名称
```python
import re
def parse_title(html):
try:
pattern = r'"title":"(.*?)"' # 正则表达式模式
title = re.search(pattern, html).group(1) # 查找匹配的字符串并提取
return title
except Exception as e:
print("解析异常:", e)
return None
# 测试接口:
url = 'https://item.taobao.com/item.htm?id=699089474285&ali_refid=a3_430673_1006:1102522391:N:%2BAkz14GC%2BXi%2Bma9dw7y9M4p5Yhq5iT8I:f9e826228fbc823934848d20e28dff81&ali_trackid=162_f9e826228fbc823934848d20e28dff81&spm=a2e0b.20350158.31919782.5'
resp_text = get_with_cookie(url)
if resp_text is not None:
title = parse_title(resp_text)
if title is not None:
# 打印商品信息
print(title)
else:
print("解析失败")
else:
print("请求失败")
```
2. 使用XPath解析商品名称
```python
from lxml import etree
def parse_title(html):
try:
root = etree.HTML(html)
title = root.xpath('//h3[@class="tb-main-title"]/text()')[0] # 使用XPath定位节点并提取文本
return title
except Exception as e:
print("解析异常:", e)
return None
# 测试接口:
url = 'https://item.taobao.com/item.htm?id=699089474285&ali_refid=a3_430673_1006:1102522391:N:%2BAkz14GC%2BXi%2Bma9dw7y9M4p5Yhq5iT8I:f9e826228fbc823934848d20e28dff81&ali_trackid=162_f9e826228fbc823934848d20e28dff81&spm=a2e0b.20350158.31919782.5'
resp_text = get_with_cookie(url)
if resp_text is not None:
title = parse_title(resp_text)
if title is not None:
# 打印商品信息
print(title.strip())
else:
print("解析失败")
else:
print("请求失败")
```
阅读全文