生成一个爬取当当网计算机书籍数据的代码
时间: 2023-09-19 20:05:50 浏览: 41
好的,以下是一个爬取当当网计算机书籍数据的 Python 代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = "http://search.dangdang.com/?key=计算机&act=input&page_index=1"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
book_list = soup.find_all(class_="pic")
for book in book_list:
title = book.find("a")["title"]
link = book.find("a")["href"]
author = book.find(class_="search_book_author").text.strip()
price = book.find(class_="price_n").text.strip()
print(title, link, author, price)
```
这个代码会爬取当当网上搜索关键词为“计算机”的书籍数据,并输出每本书的标题、链接、作者和价格。你可以根据自己的需求修改搜索关键词和其他参数。注意:爬取网站数据时请遵守相关法律法规和网站的爬虫协议。
相关问题
生成一个爬取当当网计算机书籍的代码
我可以提供一个基本框架,但请注意遵守当当网的使用条款和法律法规。
```python
import requests
from bs4 import BeautifulSoup
url = "http://category.dangdang.com/cp01.54.00.00.00.00.html" # 替换为计算机类别的URL
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
books = soup.find_all("li", class_="list_num")
for book in books:
book_name = book.find("a", class_="pic").get("title")
book_url = book.find("a", class_="pic").get("href")
book_price = book.find("p", class_="price").text
print(book_name, book_url, book_price)
```
请注意,这只是一个基本的示例代码,您需要根据实际情况进行修改和扩展。同时,请确保您的爬虫行为是合法的,不违反当当网的使用条款和法律法规。
帮我生成一个爬取上市公司年度财务数据的python代码
好的,以下是一个简单的Python爬虫示例,用于从巨潮资讯网站(http://www.cninfo.com.cn/)上爬取上市公司的年度财务数据:
```python
import requests
from bs4 import BeautifulSoup
# 待爬取的公司代码
code = '600519'
# 构造 URL
url = 'http://www.cninfo.com.cn/new/data/szse_stock{}.html'.format(code)
# 发送请求
response = requests.get(url)
# 解析 HTML
soup = BeautifulSoup(response.content, 'html.parser')
# 获取表格数据
table = soup.find_all('table')[0]
rows = table.find_all('tr')
# 输出数据
for row in rows:
cols = row.find_all('td')
cols = [col.text.strip() for col in cols]
print(cols)
```
你可以将这个示例代码作为起点,根据需要进行修改和扩展,以实现更加完善的爬虫功能。请注意,爬取网站数据需要遵循网站的规定和爬虫道德规范,不要进行恶意爬取和滥用。