美文网首页
Python爬虫简单的代码总结

Python爬虫简单的代码总结

作者: 米陽 | 来源:发表于2021-06-24 17:30 被阅读0次

这里只是一个简单的Python爬虫demo,主要是做个笔记,以后自己用到的时候,方便查询,如果有幸能帮助的小伙伴,那就更好了。
import time

from selenium import webdriver

import re

# 定义函数,参数设置为公司company, 目的是URL地址的时候接入不同的公司名称,爬取不同的公司信息

def eastmoney(company):

chrome_options = webdriver.ChromeOptions()

chrome_options.add_argument('--headless')

browser = webdriver.Chrome(options=chrome_options)

url ='https://so.eastmoney.com/news/s?keyword=' + company

browser.get(url)

data = browser.page_source

browser.quit()

p_title = '<div class="news-item"><h3><a href=".*?">(.*?)</a>'

p_href = '<div class="news-item"><h3><a href="(.*?)">.*?</a>'

p_date = '<p class="news-desc">(.*?) - .*?</p>'

title = re.findall(p_title, data)

href = re.findall(p_href, data)

date = re.findall(p_date, data, re.S)

for iin range(len(title)):

title[i] = re.sub('<.*?>', '', title[i])

date[i] = date[i].split(' ')[0]

print(str(i +1) +"." + title[i] +'  ' + date[i])

print(href[i])

companys = ['华能信托', '阿里巴巴', '腾讯控股', '京东']

for iin companys:

try:

eastmoney(i)

print(i +'该公司数据爬取成功')

except:

print(i +'该公司的数据爬取失败')

相关文章

网友评论

      本文标题:Python爬虫简单的代码总结

      本文链接:https://www.haomeiwen.com/subject/afkgyltx.html