Python攻破超强防火墙,处理超强反爬并对CSS图片背景偏移定位。
import requests
from pyquery import PyQuery as pq
from urllib.parse import urljoin
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
class Spider:
def __init__(self, url, proxy=None, user_agent=None):
self.url = url
self.proxy = proxy
self.user_agent = user_agent
self.driver = self.get_driver()
def get_driver(self):
# 这里假设你有合适的浏览器驱动程序路径
firefox_driver_path = '/path/to/geckodriver'
chrome_driver_path = '/path/to/chromedriver'
# 设置浏览器选项
firefox_options = FirefoxOptions()
chrome_options = ChromeOptions()
if self.user_agent:
firefox_options.add_argument(f'user-agent={self.user_agent}')
chrome_options.add_argument(f'user-agent={self.user_agent}')
if self.proxy:
firefox_options.add_argument(f'--proxy-server={self.proxy}')
chrome_options.add_argument(f'--proxy-server={self.proxy}')
# 返回浏览器驱动实例
try:
return webdriver.Firefox(firefox_options=firefox_options, executable_path=firefox_driver_path)
except Exception:
return webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver_path)
def get_page(self, url):
self.driver.get(url)
return self.driver.page_source
def parse_page(self, html):
doc = pq(html)
# 解析CSS中的背景图片URL
background_urls = doc('style').contents().findall(r'url<span class="katex">\("(.+?)"\)</span>')
for url in background_urls:
print(urljoin(self.url, url)) # 打印完整的图片URL
def close(self):
self.driver.close()
# 使用示例
spider = Spider('http://example.com', 'http://proxy.com', 'Mozilla/5.0')
html = spider.get_page(spider.url)
spider.parse_page(html)
spider.close()
这个示例代码展示了如何使用Selenium和PyQuery来爬取网页,并解析CSS中的背景图片URL。它假设你已经有了合适的浏览器驱动程序路径,并且可以设置代理和用户代理。这个代码片段提供了一个简单的框架,可以根据实际需求进行扩展和定制。
评论已关闭