使用python寫乙個簡單爬蟲的思路

2021-10-25 07:20:36 字數 1504 閱讀 5980

import requests

import re

from bs4 import beautifulsoup #從bs4這個庫中匯入beautifulsoup

headers =

download_url = ''

html = requests.get(download_url, headers= headers)

bhtml = beautifulsoup(html.content, "html.parser") #使用beautifulsoup解析這段**

# print(len(contents)) #列表的長度

# print(contents[0]) #說明預設是從0開始的

# 連線資料庫

conn = pymysql.connect(

host='localhost',

port=3306,

user='root',

passwd='root',

db='python',

charset='utf8'

)string=""

pic_url=""

# 獲取游標

cur = conn.cursor()

sql="insert into images values(%s,%s)"

for i in range(len(contents)): #這裡的i預設是從0開始的

if((i+1)%2==0):

string=contents[i]

print(string)

else:

pic_url=contents[i]

print(pic_url)

if i%2==0:

if i!=0:

cur.execute(sql, (string, pic_url))

print("插入資料成功")

cur.close()

conn.commit()

conn.close()

print('sql執行成功')

python寫乙個簡單爬蟲程式

python寫乙個簡單爬蟲程式 先看輸出結果 d 應用集合 python 1.py 名稱 詭秘之主 名稱 超神機械師 名稱 九星毒奶 名稱 第一序列 名稱 明天下 名稱 爛柯棋緣 名稱 虧成首富從遊戲開始 名稱 我師兄實在太穩健了 名稱 輪迴樂園 名稱 當醫生開了外掛程式 名稱 學霸的黑科技系統 名...

使用selenium寫乙個簡單的爬蟲登入郵箱

selenium使用過程比較簡單,完整 在最後 參考 參考 from selenium import webdriver import path import time 因為我的電腦不能設定環境變數,所以配置臨時環境變數,你已配置好,請忽略下面兩行 path str path.addpath d s...

乙個簡單的Python爬蟲

乙個簡單測試爬蟲的網頁 1.獲取 的html import urllib.request request urllib.request.urlopen str request.read print str 2.返回網頁標題 輸出目標網頁內title標籤內的文字 gettitle 函式返回網頁的標題,...