(104)网页爬虫

因为还没有学网络,所以就在一个文件中寻找符合规则的邮箱

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class demo2 {

    public static void main(String[] args)throws Exception {
        BufferedReader bufr=new BufferedReader(new FileReader("e:\\mail.txt"));
        String line=null;

        String regex="[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z]+){1,3}";
        Pattern p=Pattern.compile(regex);
        while((line=bufr.readLine())!=null)
        {
            Matcher m=p.matcher(line);
            while(m.find())
            {
                System.out.println(m.group());
            }
        }

    }

}
以下是一个使用Python编写的爬虫程序,可以实现从https://m.gasgoo.com/网站抓取包含上汽大众关键字的所有新闻咨询,并将新闻咨询汇总到一个Word文档中: ```python import requests from bs4 import BeautifulSoup import docx # 设置请求头 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'} # 获取网页内容 def get_html(url): try: response = requests.get(url, headers=headers) response.encoding = 'utf-8' if response.status_code == 200: return response.text else: return None except Exception as e: print(e) return None # 获取新闻列表 def get_news_list(): url = 'https://m.gasgoo.com/news/collection_104/' html = get_html(url) soup = BeautifulSoup(html, 'html.parser') news_list = soup.find_all('div', class_='news_list')[0] return news_list # 获取新闻内容 def get_news_content(news_url): html = get_html(news_url) soup = BeautifulSoup(html, 'html.parser') title = soup.find_all('h1', class_='title')[0].text content = soup.find_all('div', class_='content')[0].text.strip() return (title, content) # 将新闻咨询汇总到Word文档中 def save_to_word(news_list): doc = docx.Document() # 新建一个文档 for news in news_list: title = news.a.text if '上汽大众' in title: # 判断标题是否包含关键字 news_url = 'https://m.gasgoo.com' + news.a['href'] content = get_news_content(news_url) doc.add_heading(title, level=1) # 添加标题 doc.add_paragraph(content[0], style='Title') # 添加新闻标题 doc.add_paragraph(content[1], style='Normal') # 添加新闻内容 doc.save('上汽大众新闻.docx') # 保存文档 if __name__ == '__main__': news_list = get_news_list() save_to_word(news_list) ``` 注意事项: 1. 需要安装requests、beautifulsoup4和python-docx库,可以使用pip命令进行安装。 2. Word文档的样式可以自行设置,代码中只是简单地设置了标题和正文的样式。 3. 爬虫程序需要具有一定的抓取速度控制和异常处理机制,以防止被网站封禁IP或出现其他异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值