用java代码爬取起点中文网的热销榜中所有的小说书名
时间: 2023-05-24 18:01:56 浏览: 129
你可以使用jsoup库来处理网页爬取。以下是样例代码:
```
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("https://www.qidian.com/rank/hotsales").get();
Elements titles = doc.select(".book-mid-info h4 a");
for (Element title : titles) {
System.out.println(title.text());
}
}
}
```
这段代码连接到起点中文网的热销榜页面,使用css选择器获取所有书名标签并输出它们的文本内容。
相关问题
爬取起点中文网周票榜前三十名的书名和周票数并保存在Excel中
好的,下面是一个爬取起点中文网周票榜前三十名的书名和周票数并保存在Excel中的例子:
```
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.qidian.com/rank/hotsales'
# 发送HTTP请求,获取网页内容
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页内容,提取前三十名的数据
novel_list = []
for li in soup.select('.rank-view-list > li'):
if len(novel_list) >= 30:
break
title = li.select_one('.book-name > a').text.strip()
ticket_count = li.select_one('.week-ticket > .num').text.strip()
novel_list.append({'title': title, 'ticket_count': ticket_count})
# 将数据存储到DataFrame中
df = pd.DataFrame(novel_list)
# 将DataFrame导出为Excel文件
df.to_excel('novel_list.xlsx', index=False)
```
这个例子演示了如何爬取起点中文网周票榜前三十名的小说信息,并将小说的标题和周票数保存到Excel文件中。你可以根据自己的需求修改代码。
爬取起点中文网月票榜前三十名的书名和月票数并保存在txt中
好的,下面是一个爬取起点中文网月票榜前三十名的书名和月票数并保存在txt中的例子:
```
import requests
from bs4 import BeautifulSoup
url = 'https://www.qidian.com/rank/yuepiao'
# 发送HTTP请求,获取网页内容
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页内容,提取前三十名的数据
novel_list = []
for li in soup.select('.rank-view-list > li'):
if len(novel_list) >= 30:
break
title = li.select_one('.book-name > a').text.strip()
ticket_count = li.select_one('.month-ticket > .num').text.strip()
novel_list.append({'title': title, 'ticket_count': ticket_count})
# 将数据保存到txt文件中
with open('novel_list.txt', 'w', encoding='utf-8') as f:
for novel in novel_list:
f.write('{}\t{}\n'.format(novel['title'], novel['ticket_count']))
```
这个例子演示了如何爬取起点中文网月票榜前三十名的小说信息,并将小说的标题和月票数保存到txt文件中。你可以根据自己的需求修改代码。