- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 main_url 中抓取一些详细信息,其中每个公司都有另一个 url,并获取每个公司的凭据,其中有公司名称、电话、传真、网站等元素。我已经使用 beautifulsoup 和 requests 编写了代码,还获得了凭据(但只获得了 52 家公司)。之后它给出了 javascript 错误,因为有 loadmore 按钮。我也想通过点击那个加载更多按钮来获取所有公司的详细信息。只想处理请求和 beautifulsoup。不想为此使用 Selenium 。我会很高兴并感谢您获得帮助。这是我的代码,直到我得到好的结果,但只有加载更多按钮有问题。
from time import time
from bs4 import BeautifulSoup
import urllib.request
import requests
import json
import time
parser = 'html.parser' # or 'lxml' (preferred) or 'html5lib', if installed
resp = urllib.request.urlopen("https://www.arabiantalks.com/category/1/advertising-gift-articles")
soup = BeautifulSoup(resp, parser, from_encoding=resp.info().get_param('charset'))
cnt=0
for links in soup.find_all('a', href=True):
url=(links['href'])
# print(url)
page=requests.get(url)
# print(page).
soup=BeautifulSoup(page.content,'html.parser')
# print(soup)
results=soup.find('div',class_='rdetails')
# print(results)
if results is not None:
# print(f"company_results: {results.text}")
Name=results.find('h1')
if Name is not None:
print(f"Comapny_name: {Name.text}")
else:
print(f"Comapny_name: Notfound")
address=results.find(attrs={'itemprop':'address'})
if address is not None:
# address.text.replace(" Address : ", "")
# print(type(f"Comapny_address: {address.text}"),"this type")
print((f"Comapny_address: {address.text[12:]}"))
else:
print(f"Comapny_address: Notfound")
phone=results.find(attrs={'itemprop':'telephone'})
if phone is not None:
print(f"Comapny_phone: {phone.text[16:]}")
else:
print(f"Comapny_phone: Notfound")
fax=results.find(attrs={'itemprop':'faxNumber'})
if fax is not None:
print(f"Comapny_fax: {fax.text[7:]}")
else:
print(f"Comapny_fax: Notfound")
email=results.find(attrs={'itemprop':'email'})
if email is not None:
print(f"Comapny_email: {email.text[9:]}")
else:
print(f"Comapny_email: Notfound")
website=results.find(attrs={'itemprop':'url'})
if website is not None:
print(f"Comapny_website: {website.text}")
else:
print(f"Comapny_website: Notfound")
cnt += 1
print(cnt)
print("="*100)
如果有人知道意识形态,请复制这些代码并进行必要的修改,然后再次作为回复发布。这样我就知道要更改的地方了。我尝试了很多文章,但仍在挣扎。这是first answer我试过的最后一个链接。请帮我解决这个问题我在网络抓取方面还很新。提前致谢...
最佳答案
这个问题(几乎)是这个问题的重复:Scraping a website that has a "Load more" button doesn't return info of newly loaded items with Beautiful Soup and Selenium
不同之处在于 - 在这个实例中,ajax 响应不是 JSON,而是 HTML。您需要检查浏览器开发工具中的“网络”选项卡,以查看正在进行的网络调用。
以下代码将访问 ajax 端点,提取所有可用数据,获取所有公司简介 url、抓取名称、地址、电话、传真、电子邮件、网站并将所有内容保存到 csv 文件中:
from bs4 import BeautifulSoup
import requests
import pandas as pd
item_list = []
counter = 20
s = requests.Session()
r = s.get('https://www.arabiantalks.com/category/1/advertising-gift-articles')
soup = BeautifulSoup(r.text, 'html.parser')
items = soup.find_all('a', {'itemprop': 'item'})
for item in items:
item_list.append(item.get('href'))
while True:
payload = {
'start': counter,
'cat': 1
}
r = s.post('https://www.arabiantalks.com/ajax/load_content', data=payload)
if len(r.text) < 25:
break
soup = BeautifulSoup(r.text, 'html.parser')
items = soup.find_all('a')
for item in items:
item_list.append(item.get('href'))
counter = counter + 12
print('Total items:', len(set(item_list)))
full_comp_list = []
for x in item_list:
r = s.get(x)
soup = BeautifulSoup(r.text, 'html.parser')
c_details_card = soup.select_one('div.rdetails')
try:
c_name = c_details_card.select_one('#hcname').text.strip()
except Exception as e:
c_name = 'Name unknown'
try:
c_address = c_details_card.find('h3', {'itemprop': 'address'}).text.strip()
except Exception as e:
c_address = 'Address unknown'
try:
c_phone = c_details_card.find('h3', {'itemprop': 'telephone'}).text.strip()
except Exception as e:
c_phone = 'Phone unknown'
try:
c_fax = c_details_card.find('h3', {'itemprop': 'faxNumber'}).text.strip()
except Exception as e:
c_fax = 'Fax unknown'
try:
c_email = c_details_card.find('h3', {'itemprop': 'email'}).text.strip()
except Exception as e:
c_email = 'Email unknown'
try:
c_website = c_details_card.find('a').get('href')
except Exception as e:
c_website = 'Website unknown'
full_comp_list.append((c_name, c_address, c_phone, c_fax, c_email, c_website))
print('Done', c_name)
full_df = pd.DataFrame(list(set(full_comp_list)), columns = ['Name', 'Address', 'Phone', 'Fax', 'Email', 'Website'])
full_df.to_csv('full_arabian_advertising_companies.csv')
full_df
它还会在终端中打印出来,让您了解它在做什么:
Total items: 122
Done Ash & Sims Advertising LLC
Done Strings International Advertising LLC
Done Zaabeel Advertising LLC
Done Crystal Arc Factory LLC
Done Zone Group
Done Business Link General Trading
[....]
Name Address Phone Fax Email Website
0 Ash & Sims Advertising LLC Address : P.O.Box 50391,\nDubai - United Arab Emirates Phone Number : +971-4-8851366 , +9714 8851366 Fax : +971-4-8852499 E-mail : sales@ashandsims.com http://www.ashandsims.com
1 Strings International Advertising LLC Address : P O BOX 117617\n57, Al Kawakeb Property, Al Quoz\nDubai, U.A.E Phone Number : +971-4-3386567 , +971502503591 Fax : +971-4-3386569 E-mail : vinod@stringsinternational.org http://www.stringsinternational.org
2 Zaabeel Advertising LLC Address : Al Khabaisi, Phone Number : +971-4-2598444 Fax : +971-4-2598448 E-mail : info@zaabeeladv.com http://www.zaabeeladv.com
3 Crystal Arc Factory LLC Address : Dubai - P.O. Box 72282\nAl Quoz, Interchange 3, Al Manara, Phone Number : +971-4-3479191 , +971 4 3479191, Fax : +971-4-3475535 E-mail : info@crystalarc.net http://www.crystalarc.net
4 Zone Group Address : Al Khalidiya opp to Rak Bank, Kamala Tower,\nOffice no.1401, PO Box 129297, Abu Dhabi, UAE Phone Number : +97126339004 Fax : +97126339005 E-mail : info@zonegroupuae.ae http://www.zonegroupuae.ae
[....]
关于python - 想要通过 python 中的 beautifulsoup 使用 post 方法(不想使用 selenium)来抓取具有 loadmore 按钮的整个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73122219/
HTML 在 div 中包含字符串: 'div class="slide"' 'img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jk
我用这个方法 allcity = dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")}) 返回这样的列表: [掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆
我正在使用 Jupyter 笔记本、Python 3.5 和虚拟环境。 在我的虚拟环境中,我做了: (venv) > pip install BeautifulSoup4 这似乎运行良好 b/c 终端
我打算用 GUI 制作一个字典程序,但我在第一个障碍上就失败了。我刚刚安装了一个模块( PyDictionary ),但是当我运行以下代码时出现错误。 from PyDictionary import
我正在将一些解析器从 BeautifulSoup3 迁移到 BeautifulSoup4,我认为考虑到 lxml 非常快并且它是我在 BS4 中使用的解析器,分析它会变得多快是个好主意,这里是分析结果
这个问题在这里已经有了答案: Getting Python error "from: can't read /var/mail/Bio" (6 个答案) 关闭 11 个月前。 From Beauti
目前我无法输入这个,因为根据 top,我的处理器是 100%,我的内存是 85.7%,都被 python 占用了。 为什么?因为我让它通过一个 250 兆的文件来删除标记。 250兆,就是这样!我一直
我写了下面的代码: from bs4 import BeautifulSoup import sys # where is the sys module in the source code fold
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我正在为一个项目使用 BeautifulSoup。这是我的 HTML 结构 John Sam Bailey Jack
这段代码正确地从我的博客中提取了马拉地语文本。我很欣赏使用漂亮的汤和正则表达式是多么容易。 from bs4 import BeautifulSoup import requests, re url
我想获取 HTML 中隐藏输入字段的值。 我想用 Python 编写一个正则表达式,它将返回 fooId 的值,前提是我知道 HTML 中的行遵循以下格式 有人可以提供一个 Python 示例来解
我正在尝试获取特定月份的所有链接、标题和日期,例如网站上的三月,我正在使用 BeautifulSoup 这样做: from bs4 import BeautifulSoup import reques
我正试图通过此链接收集有关 2020 年世界上收入最高的运动员收入的信息 https://www.forbes.com/profile/roger-federer/?list=athletes这是第一
我正在尝试从带有美丽汤的网页中捕获所有相关链接。我需要的所有链接都有 class="btn btn-gray"还有文字 More Info<> 仅提取这些链接的最佳方法是什么? 最佳答案 这个怎么样?
我正在尝试抓取一个具有下拉菜单的站点,用户可以在其中选择要显示的数据的年份。但是,我似乎被困在我的实现中。 这是网站网址:https://www.pgatour.com/tournaments/mas
我正在使用 BeautifulSoup 和 mechanise 从网页中查找一些内容。问题是有时找不到我正在寻找的字符串。我不知道有什么问题 对于许多网页,它可以正常工作数月,但突然停止工作。然后我必
( 更新代码 就在下面) 我有一个类:UrlData , 生成一个 url 列表: for url in urls: rawMechSiteInfo = mech.open(url) #me
这是我到目前为止所做的事情: import requests from bs4 import BeautifulSoup URL = "https://www.google.com/search?q=
我是一名优秀的程序员,十分优秀!