【问题标题】:How do I webscrape by manipulating the URL? Python 3.5如何通过操纵 URL 进行网页抓取?蟒蛇 3.5
【发布时间】:2018-07-27 23:12:24
【问题描述】:

我想从网站this table 上抓取一张股票数据表 在我的代码中,我生成了一组股票代码。 finviz 网站的 URL 为每个特定股票生成表格,其中包含 URL 的最后部分(ei.https://finviz.com/quote.ashx?t=MBOT 和 MBOT)。我想输入我生成的数组作为 URL 的最终输入(如果我的数组是 [AAPL,MBOT] 然后https://finviz.com/quote.ashx?t=AAPL 然后https://finviz.com/quote.ashx?t=MBOT)从每个 URL 中抓取输出表并将抓取的信息输入到 CSV文件(在本例中名为“output.csv”)这是我的代码:

import csv
import urllib.request
from bs4 import BeautifulSoup

twiturl = "https://twitter.com/ACInvestorBlog"
twitpage = urllib.request.urlopen(twiturl)
soup = BeautifulSoup(twitpage,"html.parser")

print(soup.title.text)

tweets = [i.text for i in soup.select('a.twitter-cashtag.pretty-link.js-nav b')]
print(tweets)

url_base = "https://finviz.com/quote.ashx?t="
url_list = [url_base + tckr for tckr in tweets]
fpage = urllib.request.urlopen(url_list)
fsoup = BeautifulSoup(fpage, 'html.parser')

with open('output.csv', 'wt') as file:
    writer = csv.writer(file)

    # write header row
    writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2-cp'})))

    # write body row
    writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2'}))) 

这是我的错误列表

"C:\Users\Taylor .DESKTOP-0SBM378\venv\helloworld\Scripts\python.exe" "C:/Users/Taylor .DESKTOP-0SBM378/PycharmProjects/helloworld/helloworld"
Antonio Costa (@ACInvestorBlog) | Twitter
Traceback (most recent call last):
['LINU', 'FOSL', 'LINU', 'PETZ', 'NETE', 'DCIX', 'DCIX', 'KDMN', 'KDMN', 'LINU', 'CNET', 'AMD', 'CNET', 'AMD', 'NETE', 'NETE', 'AAPL', 'PETZ', 'CNET', 'PETZ', 'PETZ', 'MNGA', 'KDMN', 'CNET', 'ITUS', 'CNET']
  File "C:/Users/Taylor .DESKTOP-0SBM378/PycharmProjects/helloworld/helloworld", line 17, in <module>
    fpage = urllib.request.urlopen(url_list)
  File "C:\Users\Taylor .DESKTOP-0SBM378\AppData\Local\Programs\Python\Python36-32\Lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Taylor .DESKTOP-0SBM378\AppData\Local\Programs\Python\Python36-32\Lib\urllib\request.py", line 517, in open
    req.timeout = timeout
AttributeError: 'list' object has no attribute 'timeout'

Process finished with exit code 1

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    您将列表传递给 urllib.request.urlopen() 而不是字符串,仅此而已!所以你已经很亲近了。

    要打开所有不同的网址,只需使用 for 循环。

    for url in url_list:
    
        fpage = urllib.request.urlopen(url)
        fsoup = BeautifulSoup(fpage, 'html.parser')
    
        #scrape single page and add data to list
    
    with open('output.csv', 'wt') as file:
        writer = csv.writer(file)
    
        #write datalist
    

    【讨论】:

      【解决方案2】:

      您正在向 urlopen 方法传递一个列表。试试下面的,它会从第一个 URL 中检索数据。

      fpage = urllib.request.urlopen(url_list[0])
      fsoup = BeautifulSoup(fpage, 'html.parser')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        相关资源
        最近更新 更多