【问题标题】:Extracting multiple URLs with no 'a' or 'href' tags from web page with BS4使用 BS4 从网页中提取多个没有“a”或“href”标签的 URL
【发布时间】:2016-06-24 18:07:18
【问题描述】:

我正在使用 Selenium 制作一个简单的程序,该程序会转到 Flickr.com,搜索用户输入的术语,然后打印出所有这些图像的 URL。

我在最后一部分苦苦挣扎,只获取图像的 URL。我一直在使用class_= 搜索来获取 URL 所在的 HTML 部分。这会在搜索“苹果”时多次返回以下内容:

<div class="view photo-list-photo-view requiredToShowOnServer awake" 
   data-view-signature="photo-list-photo-view__engagementModelName_photo-lite-
   models__excludePeople_false__id_6246270647__interactionViewName_photo-list-
   photo-interaction-    view__isOwner_false__layoutItem_1__measureAFT_true__model_1__modelParams_1_    _parentContainer_1__parentSignature_photolist-
   479__requiredToShowOnClient_true__requiredToShowOnServer_true__rowHeightMod    _1__searchTerm_apples__searchType_1__showAdvanced_true__showSort_true__show    Tools_true__sortMenuItems_1__unifiedSubviewParams_1__viewType_jst"
   style="transform: translate(823px, 970px); -webkit-transform:     translate(823px, 970px); -ms-transform: translate(823px, 970px); width:
   237px; height: 178px; background-image:
   url(//c3.staticflickr.com/7/6114/6246270647_edc7387cfc_m.jpg)">
<div class="interaction-view"></div>

我想要的只是每张图片的 URL 是这样的:

c3.staticflickr.com/7/6114/6246270647_edc7387cfc_m.jpg

由于没有 ahref 标记,我正在努力将它们过滤掉。

我尝试在最后做一些正则表达式,如下所示:

print(soup.find_all(re.compile(r'^url\.jpg$')))

但这没有用。

下面是我的完整代码,谢谢。

import os
import re
import urllib.request as urllib2
import bs4
from selenium import webdriver
from selenium.webdriver.common.keys import Keys 

os.makedirs('My_images', exist_ok=True)

browser = webdriver.Chrome()
browser.implicitly_wait(10)

print("Opening Flickr.com")

siteChoice = 'http://www.flickr.com'

browser.get(siteChoice)

print("Enter your search term: ")

term = input("> ")

searchField = browser.find_element_by_id('search-field')
searchField.send_keys(term)
searchField.submit()

url = siteChoice + '/search/?text=' + term

html = urllib2.urlopen(url)

soup = bs4.BeautifulSoup(html, "html.parser")

print(soup.find_all(class_='view photo-list-photo-view requiredToShowOnServer awake', style = re.compile('staticflickr')))

我更改的代码:

p = re.compile(r'url\(\/\/([^\)]+)\)')

test_str = str(soup)

all_urls = re.findall(p, test_str)


print('Exporting to file')


with open('flickr_urls.txt', 'w') as f:
    for i in all_urls:
        f.writelines("%s\n" % i)

print('Done')

【问题讨论】:

    标签: python regex beautifulsoup bs4


    【解决方案1】:

    试试这个

    url\(\/\/([^\)]+)\)
    

    Demo

    import re
    p = re.compile(ur'url\(\/\/([^\)]+)\)')
    test_str = u"<div class=\"view photo-list-photo-view requiredToShowOnServer awake\" \ndata-view-signature=\"photo-list-photo-view__engagementModelName_photo-lite-\nmodels__excludePeople_false__id_6246270647__interactionViewName_photo-list-\nphoto-interaction-    view__isOwner_false__layoutItem_1__measureAFT_true__model_1__modelParams_1_    _parentContainer_1__parentSignature_photolist-\n479__requiredToShowOnClient_true__requiredToShowOnServer_true__rowHeightMod    _1__searchTerm_apples__searchType_1__showAdvanced_true__showSort_true__show    Tools_true__sortMenuItems_1__unifiedSubviewParams_1__viewType_jst\"\n style=\"transform: translate(823px, 970px); -webkit-transform:     translate(823px, 970px); -ms-transform: translate(823px, 970px); width:\n 237px; height: 178px; background-image:\n url(//c3.staticflickr.com/7/6114/6246270647_edc7387cfc_m.jpg)\">\n<div class=\"interaction-view\"></div>"
    
    m = re.search(p, test_str)
    print m.group(1)
    

    输出:

    c3.staticflickr.com/7/6114/6246270647_edc7387cfc_m.jpg
    

    【讨论】:

    • 谢谢,我可以看到这将如何抓住我需要的东西,但我认为我现在没有正确实施它,那么最后一行应该是这样吗? print(soup.find_all(re.compile(r'url(\/\/([^)]+))'))) 它不适合我我担心虽然
    • 谢谢!我认为它应该是 (r'url.. 而不是 (ur'url... 对吗?我已经把我的更改放在上面并且现在可以正常工作了,再次感谢
    【解决方案2】:

    使用 Selenium 废弃页面中的所有 png/jpg 链接:

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("https://www.flickr.com/")
    links = driver.execute_script("return document.body.innerHTML.match(" \
      "/https?:\/\/[a-z_\/0-9\-\#=&.\@]+\.(jpg|png)/gi)")
    print links
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多