Python爬蟲簡單的代碼總結(jié)

這里只是一個(gè)簡單的Python爬蟲demo,主要是做個(gè)筆記,以后自己用到的時(shí)候,方便查詢,如果有幸能幫助的小伙伴,那就更好了。
import time

from selenium import webdriver

import re

# 定義函數(shù),參數(shù)設(shè)置為公司company, 目的是URL地址的時(shí)候接入不同的公司名稱,爬取不同的公司信息

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 +'該公司數(shù)據(jù)爬取成功')

except:

print(i +'該公司的數(shù)據(jù)爬取失敗')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容