Không có cách nào trong selen để đọc các tiêu đề yêu cầu hoặc phản hồi. Bạn có thể làm điều đó bằng cách hướng dẫn trình duyệt của bạn kết nối thông qua một proxy ghi lại loại thông tin này.
Đặt tác nhân người dùng trong Firefox
Cách thông thường để thay đổi tác nhân người dùng cho Firefox là đặt biến
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
7 trong hồ sơ Firefox của bạn. Lưu ý rằng điều này là độc lập với selen.Bạn có thể chỉ đạo Selenium để sử dụng một cấu hình khác với hình thức mặc định, như thế này:
from selenium import webdriver
profile = webdriver.FirefoxProfile[]
profile.set_preference["general.useragent.override", "whatever you want"]
driver = webdriver.Firefox[profile]
Đặt tác nhân người dùng trong Chrome
Với Chrome, những gì bạn muốn làm là sử dụng tùy chọn dòng lệnh
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
8. Một lần nữa, đây không phải là một thứ selen. Bạn có thể gọi Chrome tại dòng lệnh với from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
9 để đặt tác nhân thành giá trị agent = driver.execute_script["return navigator.userAgent"]
0.Với selen, bạn đã đặt nó như thế này:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
Cả hai phương pháp trên đã được thử nghiệm và tìm thấy để làm việc. Tôi không biết về các trình duyệt khác.
Nhận đại lý người dùng
Selenium không có phương pháp để truy vấn tác nhân người dùng từ một thể hiện là
agent = driver.execute_script["return navigator.userAgent"]
1. Ngay cả trong trường hợp của Firefox, bạn không thể khám phá tác nhân người dùng mặc định bằng cách kiểm tra agent = driver.execute_script["return navigator.userAgent"]
2 sẽ là gì nếu không được đặt thành giá trị tùy chỉnh. [Cài đặt này không tồn tại trước khi nó được đặt thành một số giá trị.]Tuy nhiên, khi trình duyệt được khởi động, bạn có thể nhận được tác nhân người dùng bằng cách thực thi:
agent = driver.execute_script["return navigator.userAgent"]
Biến
agent = driver.execute_script["return navigator.userAgent"]
3 sẽ chứa tác nhân người dùng. Hơn như thế nàyfrom PIL import Image import glob image_list = [] for filename in glob.glob['yourpath/*.gif']: #assuming gif im=Image.open[filename] image_list.append[im]
percent_missing = df.isnull[].sum[] * 100 / len[df] missing_value_df = pd.DataFrame[{'column_name': df.columns, 'percent_missing': percent_missing}]
days = 0 week = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, 3.‘Sunday’] while day < 7: print[“Today is” + week[days]] days += 1
options = webdriver.ChromeOptions[] options.add_experimental_option["excludeSwitches", ["enable-automation"]] options.add_experimental_option['useAutomationExtension', False] options.add_argument["--disable-blink-features=AutomationControlled"] driver = webdriver.Chrome[options=options]
>>> ["foo", "bar", "baz"].index["bar"] 1
import pandas as pd sheets_dict = pd.read_excel['Book1.xlsx', sheetname=None] full_table = pd.DataFrame[] for name, sheet in sheets_dict.items[]: sheet['sheet'] = name sheet = sheet.rename[columns=lambda x: x.split['\n'][-1]] full_table = full_table.append[sheet] full_table.reset_index[inplace=True, drop=True] print full_table
def read_csv_pgbar[csv_path, chunksize, usecols, dtype=object]: # print['Getting row count of csv file'] rows = sum[1 for _ in open[csv_path, 'r']] - 1 # minus the header # chunks = rows//chunksize + 1 # print['Reading csv file'] chunk_list = [] with tqdm[total=rows, desc='Rows read: '] as bar: for chunk in pd.read_csv[csv_path, chunksize=chunksize, usecols=usecols, dtype=dtype]: chunk_list.append[chunk] bar.update[len[chunk]] df = pd.concat[[f for f in chunk_list], axis=0] print['Finish reading csv file'] return df
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
0from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
1from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
2from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
3from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
4from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
5from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options[]
opts.add_argument["user-agent=whatever you want"]
driver = webdriver.Chrome[chrome_options=opts]
6Duyệt qua các đoạn trích >>