首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是最好(最快)的方式来浏览网页?

什么是最好(最快)的方式来浏览网页?
EN

Stack Overflow用户
提问于 2019-03-23 06:12:17
回答 1查看 108关注 0票数 0

我试图从谷歌专利中收集数据,发现执行时间太长了。我怎样才能提高速度?超过8000项专利已经花了7个小时了..。

这里是专利的一个例子。

我需要从下面的表中获取数据,并将它们写入csv文件。我想瓶颈在WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='table style-scope patent-result']")))

这是必要的,还是我可以使用find_elements_by_css_selector并检查它是否返回任何内容?

代码语言:javascript
复制
#...
from selenium.webdriver.support import expected_conditions as EC
#...

##  read file of patent numbers and initiate chrome

url = "https://patents.google.com/patent/US6403086B1/en?oq=US6403086B1"

for x in patent_number:

    #url = new url with new patent number similar to above

    try: 
        driver.get(url) 
        driver.set_page_load_timeout(20) 
    except: 
        #--write to csv
        continue

    if "404" in driver.title: #patent number not found
        #--write to csv
        continue

    try: 
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//div[@class='table style-scope patent-result']"))
        )
    except: 
        #--write to csv
        continue


    ##  rest of code to get data from tables and write to csv

是否有更有效的方法来查找这些表是否存在于专利页面上?或者,如果我使用BeautifulSoup,会有什么不同吗?

我对网络抓取很陌生,所以任何帮助都是非常感谢的:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-23 07:18:22

不确定您想要的是哪些表,但是您可以使用请求和熊猫来获取表,也可以使用会话来重用连接。

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

codes = ['US6403086B1','US6403086B1'] #patent numbers to come from file
with requests.Session() as s:
    for code in codes:
        url = 'https://patents.google.com/patent/{}/en?oq={}'.format(code, code)
        r = s.get(url)
        tables = pd.read_html(str(r.content))
        print(tables)  #example only. Remove later
       #here would add some tidying up to tables e.g. dropNa rows, replace NaN with '' .... 
       # rather than print... whatever steps to store info you want until write out
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55311132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档