这是我的Python代码:
import urllib2
from bs4 import BeautifulSoup
page = urllib2.urlopen("http://www.amazon.com/")
soup = BeautifulSoup(page)
print soup
Run Code Online (Sandbox Code Playgroud)
它适用于 google.com 和许多其他网站,但不适用于 amazon.com。
我可以在浏览器中打开 amazon.com,但生成的“汤”仍然没有。
此外,我发现它也无法从 appannie.com 上抓取。然而,代码并没有给出任何结果,而是返回一个错误:
HTTPError: HTTP Error 503: Service Temporarily Unavailable
Run Code Online (Sandbox Code Playgroud)
所以我怀疑亚马逊和 App Annie 是否会阻止抓取。
我希望从 sec.gov 网站上抓取大量文件,到目前为止进展顺利。问题是旧文件采用 .txt 格式,并且没有任何真正的 HTML 格式。有什么方法可以使用Python从这些文件中获取信息吗?
我有大约 30,000 个这样的人要做,而旧文档是我老板真正想要的......我目前正在使用 BeautifulSoup4 来处理其他格式正确的片段。
提前致谢!
我正在尝试从某个网站检索链接并将其保存到列表中以供进一步使用.到目前为止我得到的(这是我存储输出的.txt文件):
<ul>
<li><a class="active" href="/review/huerco-s-those-you-who-have-never-and-also-those-w/">Huerco S.<span><i>For Those Of You Who Have Never (And Also Those Who Have)</i></span></a></li>
<li><a href="/review/dam-funk-dj-kicks/">Dâm-Funk<span><i>DJ-Kicks</i> </span></a></li>
<li><a href="/review/skepta-konnichiwa/">Skepta<span><i>Konnichiwa</i></span></a></li>
<li><a href="/review/jessy-lanza-oh-no/">Jessy Lanza<span><i>Oh No</i></span></a></li>
<li><a href="/review/radiohead-moon-shaped-pool/">Radiohead<span><i>A Moon Shaped Pool</i></span></a></li>
<li><a href="/review/brodka-clashes/">Brodka<span><i>Clashes</i></span></a></li>
<li><a href="/review/james-blake-colour-anything/">James Blake<span><i> The Colour In Anything</i></span></a></li>
<li><a href="/review/kamaiyah-good-night-ghetto/">Kamaiyah<span><i>A Good Night In The Ghetto</i></span></a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我想将这些特定链接提取到列表中.我的代码:
website = "http://www.porcys.com/review/"
openWebsite = soup(urllib.request.urlopen(website), 'html.parser')
reviews = openWebsite.find(name="section", attrs={'class': 'slider-content review'}).ul
for a in reviews(href = True):
temp = str("http://www.porcys.com"+str(a['href']))
print(temp)
Run Code Online (Sandbox Code Playgroud)
打印输出正是我想要的(这意味着 - 只是链接,没有任何HTML标签),但我将它移动到列表有困难.我试过了
results[a] = temp
a+=1
Run Code Online (Sandbox Code Playgroud)
但我得到"列表索引必须是整数或切片,而不是标签"
我是网络爬虫的新手,我想获取页面的html。但是当我运行程序时,我得到的html为空,控制台显示了javascript
from bs4 import BeautifulSoup
import requests
import urllib
url = "https://linkedin.com/company/1005"
r = requests.get(url)
html_content = r.text
soup = BeautifulSoup(html_content,'html.parser')
print (soup.prettify())
Run Code Online (Sandbox Code Playgroud)
我试着写入文件:
f = open('parsed.txt', 'w')
f.write(url + '\n' + title + '\n' + email + '\n\n')
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Traceback (most recent call last):
File "C:/Users/Brand Cream/AppData/Local/Programs/Python/Python35/index.py", line 122, in parsePage
f = open('parsed.txt', 'w')
TypeError: open() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
怎么了?
我尝试使用Python中的Beautifulsoup解析50 000个url.解析在循环中工作:
我发现解析一个页面的时间是15-18秒.从第一页开始,我会抓住20个元素.
为什么Beautifulsoup工作这么慢?如何在Python中加速Beautifulsoup?
我一直在使用在pyhton2.7中使用BeautifulSoup的搜寻器,但遇到此错误:
AttributeError:“功能”对象没有属性“ urljoin ”
它实际上在一行中:
first_link = urlparse.urljoin('https://en.wikipedia.org/', article_link)
我已经使用urlparse导入了urljoin
from urlparse import urljoin
Python 新手,我尝试使用 BeautifulSoup 使用以下代码从 etherscan.com 网页中提取“ETH 余额”:
import bs4, requests
res = requests.get('https://etherscan.io/address/0x93673eeed88fda9423b8037374164383df54aec1')
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
ethBal = soup.find("td", text="ETH Balance").find_next("td").text
print('The ETH blance is '+ ethBal)
Run Code Online (Sandbox Code Playgroud)
但是我不断收到错误消息:
Traceback (most recent call last):
File "/Users/tfountain/Desktop/python_work/c2.py", line 7, in <module>
ethBal = soup.find("td", text="ETH Balance").find_next("td").text
AttributeError: 'NoneType' object has no attribute 'find_next'
Run Code Online (Sandbox Code Playgroud)
我哪里出错了,获得 ETH 余额的最佳方式是什么?
python的新增功能,在尝试设置一些代码以从网页列表中抓取数据时会遇到错误。
这些页面之一的链接是-https://rspo.org/members/2.htm ,我正在尝试获取诸如“会员编号”,“类别”,“部门”,“国家”,等等,然后将其全部导出到电子表格中。
码:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
import requests
pages = []
for i in range(1, 10):
url = 'https://rspo.org/members/' + str(i)
pages.append(url)
for item in pages:
page = requests.get(item)
soup = soup(page.text, 'html.parser')
member = soup.find_all("span", {"class":"current"})
Run Code Online (Sandbox Code Playgroud)
我得到以下错误:
追溯(最近一次通话):
文件“”,第3行,在汤=汤中(page.text,'html.parser')
TypeError:“ ResultSet”对象不可调用
不知道为什么我得到这个错误。我尝试查看Stack Overflow上的其他页面,但似乎没有任何类似的错误出现在上面。
我已经在python中创建了一个脚本,以从网站中获取不同帖子的标题,并且可以完美地捕获它们。
但是,我现在希望此脚本执行的操作是记住上一次抓取的结果,以便当我运行两次时,它不会获取相同的结果。更清楚地说-脚本将在第一次执行时照常解析结果,但在找不到新帖子之前,它将在后续执行中无法获取相同的结果。
使用csv:
import csv
import requests
from bs4 import BeautifulSoup
def get_posts(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")
for item in soup.select(".summary .question-hyperlink"):
yield item.text
if __name__ == '__main__':
link = '/sf/ask/tagged/web-scraping/'
with open("output.csv","w",newline="") as f:
writer = csv.writer(f)
for item in get_posts(link):
writer.writerow([item])
print(item)
Run Code Online (Sandbox Code Playgroud)
使用数据库:
import mysql.connector
from bs4 import BeautifulSoup
import requests
url = "/sf/ask/tagged/web-scraping/"
def connect():
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd = "",
database="mydatabase"
)
return mydb
def create_table(link):
conn = connect()
mycursor …Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×10
web-scraping ×4
python-3.x ×3
python-2.7 ×2
html ×1
linkedin ×1
parsing ×1