怎么用python爬取一个网站的网页数量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用python爬取一个网站的网页数量相关的知识,希望对你有一定的参考价值。
参考技术A 1.这个要根据你的网站地址进行分析,构造网站的url,通过for循环,做统计输出,从而计算出一个网站的网页数量。
2.
由于你未给出具体网站的地址,只能给你说个流程如上。
望采纳,希望能帮到你。。。。。。
怎么用python爬需要登录的网站数据
参考技术A scrapy.FormRequestlogin.py
class LoginSpider(scrapy.Spider):
name = 'login_spider'
start_urls = ['hin.com'] def parse(self, response):
return [
scrapy.FormRequest.from_response(
response, # username和password要根据实际页面的表单的name字段进行修改
formdata='username': 'your_username', 'password': 'your_password',
callback=self.after_login)] def after_login(self, response):
# 登录后的代码
pass123456789101112131415
selenium登录获取cookie
get_cookie_by_selenium.py
import pickleimport timefrom selenium import webdriverdef get_cookies():
url = 'httest.com'
web_driver = webdriver.Chrome()
web_driver.get(url)
username = web_driver.find_element_by_id('login-email')
username.send_keys('username')
password = web_driver.find_element_by_id('login-password')
password.send_keys('password')
login_button = web_driver.find_element_by_id('login-submit')
login_button.click()
time.sleep(3)
cookies = web_driver.get_cookies()
web_driver.close() return cookiesif __name__ == '__main__':
cookies = get_cookies()
pickle.dump(cookies, open('cookies.pkl', 'wb'))12345678910111213141516171819202122232425
获取浏览器cookie(以Ubuntu的Firefox为例)
get_cookie_by_firefox.py
import sqlite3import pickledef get_cookie_by_firefox():
cookie_path = '/home/name/.mozilla/firefox/bqtvfe08.default/cookies.sqlite'
with sqlite3.connect(cookie_path) as conn:
sql = 'select name,value from moz_cookies where baseDomain="test.com"'
cur = conn.cursor()
cookies = ['name': name, 'value': value for name, value in cur.execute(sql).fetchall()] return cookiesif __name__ == '__main__':
cookies = get_cookie_from_firefox()
pickle.dump(cookies, open('cookies.pkl', 'wb'))12345678910111213141516
scrapy使用获取后的cookie
cookies = pickle.load(open('cookies.pkl', 'rb'))yield scrapy.Request(url, cookies=cookies, callback=self.parse)12
requests使用获取后的cookie
cookies = pickle.load(open('cookies.pkl', 'rb'))
s = requests.Session()for cookie in cookies:
s.cookies.set(cookie['name'], cookie['value'])1234
selenium使用获取后的cookie
from selenium import webdriver
cookies = pickle.load(open('cookies.pkl', 'rb'))
w = webdriver.Chrome()# 直接添加cookie会报错,下面是一种解决方案,可能有更好的# -- start --w.get('hww.test.com')
w.delete_all_cookies()# -- end --for cookie in cookies:
w.add_cookie(cookie)
以上是关于怎么用python爬取一个网站的网页数量的主要内容,如果未能解决你的问题,请参考以下文章